Thread: converting hex to binary

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    converting hex to binary

    Hello, I have to design a program which takes 4 integer values from the user as well as a 2 digit hex value. The program then performs some arithmetic based upon that hex value's binary equivalent. I know how to write most of the program, however, I am not sure how to convert my hex value into binary. I read it in using %x and store that value in an unsigned int. How do I take this value and convert it to binary or does storing it as an unsigned int already handle that ?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Brian!

    Since it's the middle of the night and I'm FRIED, I'll let someone with more active brain cells respond. You might want to read up on "two's compliment", in the meantime.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It depends on what you mean by "convert the hex value into binary".

    Format specifiers (%d, %x) change how the data is read from a file (or keyboard). They don't change how an unsigned int represents a value. So
    Code:
        unsigned x,y;
        scanf("%u %x", &x, &y);
    then, if you input "175 AF", x and y will both receive the same value (175). The difference is in how input data is processed to extract the value. The unsigned type supported by your compiler will represent the value 175 in the same manner, regardless of whether it was read as 175 (using the %u format) or as AF (using the %x format).

    If you want to print the binary digits, then you need to compute the value of each digit before outputting it. The usual technique involves determining if the value is odd, and dividing by 2, repeatedly until all binary digits are printed.

    But you don't need to do anything to the value read if you use the correct format specifier. That is the purpose of format specifiers .... to map between a value and output (or input) you need.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    well I don't want to print the binary, rather once I have all 8 binary digits, I want to manipulate them bit shift operators. This part I know how to do. you're saying the best way to get the 2 digit Hex into binary is with that division method, correct ?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No. Read my post again. I said your question could have more than one meaning.

    If you want to get input into a variable, use a format specifier. Then use bit shift operators on the variable. That's it.

    The only exception is if you want to print each binary digit (i.e. print a string of 0's and 1's) - which you've now just said is not what you want to do.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    that makes perfect sense now. thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Decimal to Binary
    By BeldenML in forum C Programming
    Replies: 17
    Last Post: 02-09-2012, 11:29 AM
  2. Converting hex to binary
    By suryap.kv1bbsr in forum C++ Programming
    Replies: 8
    Last Post: 01-17-2011, 08:34 AM
  3. Converting From Binary Tree to Threaded Binary Trees
    By elton_fan in forum C Programming
    Replies: 15
    Last Post: 11-08-2007, 11:41 PM
  4. converting text to binary
    By nhallahan in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2006, 04:55 PM
  5. Converting decimal to binary?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-17-2002, 08:21 AM

Tags for this Thread