Thread: Hex to Binary Converter

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Los Angeles, CA
    Posts
    2

    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. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    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. #3
    Registered User
    Join Date
    Sep 2012
    Location
    Los Angeles, CA
    Posts
    2
    Quote Originally Posted by root4 View Post
    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. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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.
    Last edited by Subsonics; 09-24-2012 at 05:21 PM. Reason: far left, not far right. :)

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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.
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Subsonics View Post
    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 .
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by vart View Post
    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. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Subsonics View Post
    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. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by christop View Post
    "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. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Subsonics View Post
    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. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by christop View Post
    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. #13
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Subsonics View Post
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary to Hexa converter c++ HELP
    By smile213 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2011, 08:28 AM
  2. Binary Converter - Bit level
    By Eman in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2010, 09:40 AM
  3. Decimal to Binary Converter
    By peckitt99 in forum C Programming
    Replies: 16
    Last Post: 10-12-2006, 05:25 AM
  4. binary converter
    By cerin in forum C++ Programming
    Replies: 5
    Last Post: 04-18-2005, 04:52 PM
  5. Binary converter
    By CheesyMoo in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2003, 10:27 PM