# dec -> bin -> hex in arrays

This is a discussion on dec -> bin -> hex in arrays within the C Programming forums, part of the General Programming Boards category; I have some numbers stored in a character array. Assume length of 3 arrays is: dec[2]; bin[6]; hex[2]; Now if ...

1. ## dec -> bin -> hex in 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?

2. search -> forums -> google

Quzah.

3. 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.
Code:
`char dec[3]`
Then make dec[2] = '\0'

Now you can use strtol() to convert this to a long:
Code:
```   long num;
num = strtol(dec, NULL, 10);```
Now to store in a char array as hex, use sprintf():
Code:
`   sprintf(hex,"%X",num);`
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.