I have some numbers stored in a character array. Assume length of 3 arrays is:
dec[2];
bin[6];
hex[2];
Now if I have 34 in the dec array i.e. dec[0]=3 and dec[1]=4, how can I convert this into binary and then into hex to fill the other 2 arrays?
I have some numbers stored in a character array. Assume length of 3 arrays is:
dec[2];
bin[6];
hex[2];
Now if I have 34 in the dec array i.e. dec[0]=3 and dec[1]=4, how can I convert this into binary and then into hex to fill the other 2 arrays?
search -> forums -> google
Quzah.
Hope is the first step on the road to disappointment.
Here's one idea.
>dec[2];
First make this long enough so you have can use it as a string. To make room for the string terminator, make it at least three char.
Then make dec[2] = '\0'Code:char dec[3]
Now you can use strtol() to convert this to a long:
Now to store in a char array as hex, use sprintf():Code:long num; num = strtol(dec, NULL, 10);
There is no library function to convert to binary. To store it in a char array as binary, so you could use a loop to strip each binary digit.Code:sprintf(hex,"%X",num);