0

Hi,

I'm trying to figure out how to convert a hex number (that I read in from a file) to an 8 bit binary number. At the minute I'm converting the number from Hex to decimal to binary (which probably isn't the best way). The only problem I have with this though is that if the hex number i read in is small it doesn't use all the bits.

So basically I want to be able to stick a few zeros in front of the smaller binary numbers.

I've included a simpler example of the code I'm using. For example the code below converts

0x39 to 111001, but I want 00111001.

Any help you could offer would be much appreciated.

Thanks

pmee

```
/* Libraries */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int j;
char binaryNum[8], *pointer;
char hexNum[8] = "39";
/* From Hex convert to decimal */
j = strtol( hexNum, &pointer, 16);
/* From Decimal convert to Binary */
itoa(j, binaryNum, 2);
printf("Hex: %s Bin: %s \n",hexNum, binaryNum );
system("PAUSE");
return 0;
}
```