More help in C: Making Hex numbers into Binary

uCsDNerd

Senior member
Mar 3, 2001
338
0
0
Is there some sort of library command that will take care of this conversion for me? Else, what's the basic method for doing this conversion?

Thanks.

EDIT:: Nevermind. I'm dumb. All I need to do is shift the stupid thing over. Thanks for the help though everyone.
 

Tycho987

Junior Member
Mar 4, 2002
2
0
0
Well, I don't know of any program to do it with, so the only other way I know of is to convert the Hex into Decimal and then convert the Decimal to Binary.
 

hoov

Senior member
Nov 5, 1999
210
0
0
If you are useing windows you can use the calculator program to convert hex and bin numbers.You will find under accessories open the program and hit the view tab and then check scientific
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0


<< so the only other way I know of is to convert the Hex into Decimal and then convert the Decimal to Binary. >>



Hmm? Convert hex into decimal, then to binary? Any given numeric value can be /represented/ in hex, or decimal, or octal, ad nauseum. If an numeric value is represented in hex, it's still a number. If it's represented in decimal, it's still the same number. Binary is just another representation, there's no need to convert between any of the bases. Indeed, when you represent a number in hex, then in decimal, you aren't converting anything. Here's an example of how you would print the binary representation of a byte...

char x = 1;
int n;
for(n=0; n<8; n++)
{
if((x & 0x80) !=0)
{
printf("1");
}
else
{
printf("0");
}
if (n==3)
{
printf(" ");
}
x = x<<1;
}

It'll put a space between the nibbles (a nibble is 4 bits). A little explanation of how it works:

Lets say the byte (x) is numeric 1 (lets keep it simple). x, in binary, would look like...

0000 0001

In the for loop you see, n starts out at 0, and goes to 7. So, in the first iteration of the loop, n, in binary, looks like...

0000 0000

It will bitwise AND x with 0x80. 0x80 in binary looks like...

1000 0000

So, 00000000 AND 10000000 == 0, so it will print a 0 (as it should). It will then shift x left on bit position, giving you...

0000 0010

The bitwise AND will still == 0 as 00000010 AND 10000000 == 0. It will keep shifting left until it's reached the byte boundary. When n == 7, x will have been shifted giving you...

x = 10000000

ANDing x with 0x80 gives you 10000000 AND 10000000 == 255 (i.e. >0), so it'll print a 1. The result should be 0000 0001, which is what you wanted.

Hope that made sense.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
You can use sscanf to convert from hex to decimal.
Then something like descartes' algorithm can give you the binary representation.
 

kylef

Golden Member
Jan 25, 2000
1,430
0
0
Are you attempting to convert a text string of hex digits into a binary number? There is almost certainly an easy way to do what you want to do, but you need to be more specific.
 

uCsDNerd

Senior member
Mar 3, 2001
338
0
0
I'm trying to convert a string of hex numbers ie: 7ff3... into a string of binary numbers using C. The reason behind this is that I don't just want to display a binary representation of this number, I must use this number to locate a memory address.

To get more into detail.....
I'm given a 32 bit sequence of numbers represented in hex. Depending on the user given commands of the program , I have to determine which bits represent the address of the memory location, and which bit determines that data of that location. Thus, depending on user input, the address can either be 7 bits, 10 bits, 4 bits. . . any amount of bits, and since the given value is in hex, I need to break it down to binary to access a number of bits not divisible by 4.

So, anyone know a good way around this? thanks again!
 

uCsDNerd

Senior member
Mar 3, 2001
338
0
0
ERRRR.... nevermind the question. I was being totally stupid. I've figured it out... all i need to do is shift the hex numbers over! dOh!