# Hex to Binary Converter

This is a discussion on Hex to Binary Converter within the C Programming forums, part of the General Programming Boards category; Hey guys! I was wondering if you could help me out with the thought process behind converting a Hex character ...

1. ## Hex to Binary Converter

Hey guys! I was wondering if you could help me out with the thought process behind converting a Hex character into binary. The way it's given as input would be as a char like 0x35. From my perspective the best way to go about it would be to convert each number backwards and stop when the input value is x.

So with an input 0x35 I'd work my way backward and convert 5 to 0101 and 3 to 0011 then conjoin them and print out 00110101. But I'm wondering if this is indeed the best method to use, and exactly how I can get the last digit of a character. I know that you can use modulus 10 to grab the last digit of an int multiple times, but I'm not sure how to go about it with a char.

2. You could try that method indeed but then you would have a parsing issue plus a printing issue (printing binary). You could directly read the value as an integer (you can specify that hexadecimal base to scanf) and focus on the printing part, i.e. a function that takes an integer (whatever size it is) and prints it in binary.

3. Originally Posted by root4
You could try that method indeed but then you would have a parsing issue plus a printing issue (printing binary). You could directly read the value as an integer (you can specify that hexadecimal base to scanf) and focus on the printing part, i.e. a function that takes an integer (whatever size it is) and prints it in binary.
I get the second half of what you're saying to convert the integer to binary, but the first half I'm afraid I can't do within my restrictions, unless I'm misunderstanding what you're proposing.

In my circumstances I'm being forced to take the char as an argument to my converting function, let's call it binary_print(char input). So no scanf involved. I may have omitted that detail in my original post, sorry. So no matter what I have to deal with this char value 0x35 and manipulate it from there.

4. It's still unclear what kind of data you will be receiving from the user. Does he type "0x35"? That is 4 characters? Into a string? Then the function binary_print() needs to take a char *str as argument.

5. If your number is a string, then use strtol. It will accept both 0x prefix and 0 prefix (for octal) as well as scientific notation like 1.23e4. Once you have your number stored in an integer type you don't really have to worry about representation anymore. You can use the approach you suggested and test the least significant bit with either modulus or a bitmask with the & operator, then reduce the number by the shift operator ">>".

Doing this make the number come out in reverse though, the least significant bit will appear far left. Another approach is to use a mask that you reduce and start by masking the most significant bit (for a 1 byte number that would be 0x80 for example), then shift the mask to the right until nothing is left of it.

6. One thing that you need to realise is that a HEX number is in binary already

Think about the order that you want things to print out on the screen

0x35 = bx00110101
Printing out to screen
0
0
1
1
0
1
0
1

You need to test if the MSB is set: Print out a 1 if it is set and a 0 if it isn't set.

If you then use a shift left (<<), the second MSB is now in the MSB's place -> You can use the same test

You should do this for every bit.

7. Originally Posted by Subsonics
If your number is a string, then use strtol. It will accept both 0x prefix and 0 prefix (for octal) as well as scientific notation like 1.23e4.
The last is double not long
so strtol will stop parsing on .

8. Originally Posted by vart
The last is double not long
so strtol will stop parsing on .
That is true, strtof will. But 1.23e4 is 12300 so it could be stored in an integer just as well.

9. Originally Posted by Subsonics
That is true, strtof will. But 1.23e4 is 12300 so it could be stored in an integer just as well.
"1.23e4" can be converted to a double value (with strtof or strtod), which can then be cast to an integer as 12300, but strtol would return the long int value 1 if you pass it that string. Try it!

10. Originally Posted by christop
"1.23e4" can be converted to a double value (with strtof or strtod), which can then be cast to an integer as 12300, but strtol would return the long int value 1 if you pass it that string. Try it!
You are missing the point! strtol will not parse scientific notation numbers, the parsing stops at '.' I agreed with this. But, 1.23e4 is not a floating point value it's 12300 ie an integer.

11. Originally Posted by Subsonics
You are missing the point! strtol will not parse scientific notation numbers, the parsing stops at '.' I agreed with this. But, 1.23e4 is not a floating point value it's 12300 ie an integer.
Ok, it's good that we agree on strtol. What are you suggesting, then? If 1.23e4 is found in C source code, it's a double value. Even 0.0 is a double value.

0.0 => double
0.0f => float
0f => float
0 => int

In a purely mathematical sense, 1.23e4 is an integer, because it has no fractional part, but we're talking about C here.

12. Originally Posted by christop
Ok, it's good that we agree on strtol. What are you suggesting, then? If 1.23e4 is found in C source code, it's a double value. Even 0.0 is a double value.

0.0 => double
0.0f => float
0f => float
0 => int

In a purely mathematical sense, 1.23e4 is an integer, because it has no fractional part, but we're talking about C here.
Ah, I see, you just want to argue.

We are talking about the input to one of the strto* functions, so the input would be a string, not something found in C source (unless it's also a string of course). What extra information does 0.000 have over 0.0 or 0?

I chose 1.23e4 on purpose because the value is an integer (in a mathematical sense) and can be stored in an int without loss of information. I answered the question from the top of my head, and remembered wrong about what representations strtol accepts. That's all.

13. Originally Posted by Subsonics
Ah, I see, you just want to argue.
Nah, I'm not trying to argue right now. Sorry if I came across as argumentative. I'm just trying to figure out what you're saying because it wasn't clear and it seemed to me that you were contradicting yourself.

I chose 1.23e4 on purpose because the value is an integer (in a mathematical sense) and can be stored in an int without loss of information. I answered the question from the top of my head, and remembered wrong about what representations strtol accepts. That's all.
Ok, I see what you were saying now.

I actually had to look at the man page for strtol to see if it can take a number in scientific notation.

So, back to the topic now?