1.11M Members

Explaining the "0x30"

 
0
 

Can someone explains what specific "0x30" does in the decimal to binary function below:

void dec2bin(long decimal, char *binary)
{
int k = 0, n = 0;
int neg_flag = 0;
int remain;
char temp[80]; // take care of negative input
if (decimal < 0)
{
decimal = -decimal;
neg_flag = 1;
}
do
{
remain = decimal % 2;
decimal = decimal / 2; // whittle down decimal
temp[k++] = remain + 0x30; // makes characters 0 or 1
} while (decimal > 0);
if (neg_flag)
temp[k++] = '-'; // add back - sign
else
temp[k++] = ' '; // or space
while (k >= 0)
{
binary[n++] = temp; // reverse spelling
}
binary[n-1] = 0; // end with NULL
}

 
0
 

0x30 is hexidecimal value of 48 decimal (use your scientific calculator to convert 30 hex to decimal). Adding 48 to a numeric digit will make it a printable digit -- see any ascii conversion chart.

 
0
 

i understand what you say but i still did not understant what it does in the specific function......:?:

 
0
 

temp variable is a character array. before adding the binary digit to temp the digits must be converted to one of the ascii characters '0' through '9'. for example: 1 + 48 = 49, and if you look at that ascii chart the number 49 is the letter '1'. Also note that the number 1 and the letter '1' are not the same thing.

 
0
 

Ohh! I see....Thanks a lot for the help! Keep up the good work.

THANKS!!!!!!!!!!!!!!!!!!!!!

 
0
 

Though it would have been simple if they had something like:

// more simple to understand and no need of keeping in mind the 
// ASCII equivalent of '0'
temp[k++] = remain + '0' ;
Question Answered as of 7 Years Ago by Ancient Dragon and ~s.o.s~
 
0
 

Also, more portable, since you're not making assumptions about character set.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article