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
}

## Featured Replies in this Discussion

• 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 ... .
• 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.

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.

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

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.

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

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

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 8 Years Ago by Ancient Dragon and ~s.o.s~

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

You