C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 30th, 2012, 06:15 PM
lisa92 lisa92 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 43 lisa92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
Hex to decimal conversion

The conversion doesn't work when I use lower case letters. I'm not sure if I have to do -7 or something else.

Code:

// Program accepts an 8 digit hexadecimal input and outputs
// the corresponding value in decimal

#include <stdio.h>

main()
{

    int decimal = 0; //integer for the final decimal number
    int bit; //integer representing numbers between 0-9 and letter a-f in hex number
    char hexnumber[100]; //a char array containing the input hex number

    int i=0,j=0;

    printf("Enter your hexadecimal number: ");
    scanf("%s",&hexnumber);

    //the integer i takes the length of the input array
    i =strlen(hexnumber);

    //while there is a next bit in the array
    while(i!=0)
    {
    bit = hexnumber[j];

    //if the bit is a digit do the following
    if(('0' <= bit && bit <= '9'))
    {
    decimal = decimal * 16;
    decimal = decimal + (bit - '0');
    }

    //if the bit is a letter do the following
    if(('a' <= bit && bit <= 'z'))
    {
    decimal = decimal * 16;
    decimal = decimal + (bit - '7');
    }

    i--;
    j++;

    }

    printf("The decimal representation of this number is : %d\n",decimal);

}

Reply With Quote
  #2  
Old September 30th, 2012, 08:41 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
Code:
    //if the bit is a letter do the following
    if(('a' <= bit && bit <= 'z'))
    {
    decimal = decimal * 16;
    decimal = decimal + (bit - '7');
    }
Only the letters a through f are valid hexadecimal digits. More importantly, what in the heck is bit - '7'?

Reply With Quote
  #3  
Old September 30th, 2012, 10:56 PM
WaltP WaltP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 19 WaltP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 26 m 27 sec
Reputation Power: 0
What is the binary value of '0'?
What is the binary value of 'A'?
What is the binary value of 'a'?

If '0' must become 0, what do you have to subtract?
If 'A' must become 10, what do you have to subtract?
If 'a' must become 10, what do you have to subtract?

Reply With Quote
  #4  
Old October 1st, 2012, 09:18 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
uh, why don't you just use scanf("%x",&space_for_an_integer);
or is the 8 digit hex number permitted to be a fraction?
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #5  
Old October 2nd, 2012, 04:42 AM
WaltP WaltP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 19 WaltP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 26 m 27 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
uh, why don't you just use scanf("%x",&space_for_an_integer);
or is the 8 digit hex number permitted to be a fraction?

In what way does using this suggestion teach how to convert values from one base to another? What if the next assignment is to convert to binary. Or base 6, base 20, base 36?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Hex to decimal conversion

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap