October 2nd, 2012, 09:11 AM

Hex to decimal conversion works for upper case not lower case
The program should work for lower case letters but I don't know how to implement it. Thanks for any help.
Code:
#include <stdio.h>
main()
{
int decimal = 0; //integer for the final decimal number
int bit; //integer representing numbers between 09 and letter af 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 <= 'F'))
{
decimal = decimal * 16;
decimal = decimal + (bit  '7');
}
i;
j++;
}
printf("The decimal representation of this number is : %d\n",decimal);
}
October 2nd, 2012, 09:26 AM

> if(('A' <= bit && bit <= 'F'))
So add another if statement which also tests 'a' and 'f'
> decimal = decimal + (bit  '7');
It might make more sense (and more obvious to implement af) if you write it as
decimal = decimal + (bit  'A' + 10);