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;
}
5
Contributors
4
Replies
8
Views
8 Years
Discussion Span
Last Post by iAmLast

Change your printf line to this:

printf ( "Hex: %s  Bin: %08s\n", hexNum, binaryNum );

I have code Converting "Decimal" to "Binary".

#include <stdio.h>

void dec_bin(int num);

int main(void)
{
int m = 0;

printf("Digit (0-255): ");
scanf("%d", &m);

dec_bin(m);

return 0;
}

void dec_bin(int num)
{
int x, y;
x = y = 0;

for(y = 7; y >= 0; y--)
{
x = num / (1 << y);
num = num - x * (1 << y);
printf("%d", x);
}

printf("\n");
}

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

Check the length, while(binary.length mode eight != 0 ) addToFront('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;
}

this is a decimal to binary

void dec2bin(int x)
{

int i;
for(i = 15 ; i >=0; i--)
printf("%d", x>>i & 1);

}

so for example: x = 5
it will print
0000000000000101

if you count the total number of 0s and 1s = 16 = 15 to 0
get the idea?

hope it helps

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.