Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Binary to hex conversion proof

  1. Oct 19, 2009 #1
    Hello PF members!

    I am interested in proving that the conversion of binary to hex is valid.

    Example: 1000100100110111

    Breaking into 'quartets' 1000 1001 0011 0111

    Now using the fact that 24 = 0...15

    1000 = 8, 1001 = 9, 0011 = 3, 0111 = 7. So


    I can create the table starting from 0000 to 1111. I need to prove that this conversion really works.

    I would start the proof by stating that the number in binary form is:


    If n mod 4 == 0 (if the remainder of the division of n with 4 is zero) then:

    [tex]b_1b_2b_3b_4 .... .... .... b_{n-3}b_{n-2}b_{n-1}b_n[/tex]

    But how to prove afterward?

    else we can add m zeroes so that (m+n) mod 4 ==0.

    By adding zeroes before the binary number I can easily prove that nothing is changed.

    [tex](*)=b_1b_2...b_{n-1}b_n=b_n*2^0 + b_{n-1}*2^1+...+b_2*2^{n-2}+b_1*2^{n-1}[/tex]

    [tex]00..00b_1b_2...b_{n-1}b_n=b_n*2^0 + b_{n-1}*2^1+...+b_2*2^{n-2}+b_1*2^{n-1}+0*2^n+0*2^{n+1}...+0*2^{n+m}=(*)[/tex]

    Any help would be appreciated.
  2. jcsd
  3. Oct 20, 2009 #2
    This is not a proof but an example to show how you might think of it (I reversed the numbering of the coefficients in your example, to match the exponents):

    A 12-digit binary number: [tex]b_{11}2^{11}+b_{10}2^{10}+b_{9}2^{9}+b_{8}2^{8}+b_{7}2^{7}+b_{6}2^{6}+b_{5}2^{5}+b_{4}2^{4}+b_{3}2^{3}+b_{2}2^{2}+b_{1}2^{1}+b_{0}2^{0}[/tex]

    Factor out multiples of 16: [tex]2^8(b_{11}2^{3}+b_{10}2^{2}+b_{9}2^{1}+b_{8}2^{0})+2^4(b_{7}2^{3}+b_{6}2^{2}+b_{5}2^{1}+b_{4}2^{0})+2^0(b_{3}2^{3}+b_{2}2^{2}+b_{1}2^{1}+b_{0}2^{0})[/tex]

    Rewrite with powers of 16: [tex]16^2(b_{11}2^{3}+b_{10}2^{2}+b_{9}2^{1}+b_{8}2^{0})+16^1(b_{7}2^{3}+b_{6}2^{2}+b_{5}2^{1}+b_{4}2^{0})+16^0(b_{3}2^{3}+b_{2}2^{2}+b_{1}2^{1}+b_{0}2^{0})[/tex]

    Everything in parentheses is a hex digit, with value 0-15.
  4. Oct 20, 2009 #3
    Ohh.... Thanks a lot.

    Here is what I've done:

    (I've excluded the case which I have already proof with the zeroes before the number)

    So lets suppose there are m+n+1 digits, and we can form a quartets.

    The binary number is:

    [tex]b_{m+n}2^{m+n} + b_{m+n-1}2^{m+n-1} + ... + b_32^3+b_22^2+b_12^1+b_02^0[/tex]

    Now factoring out multiples of 16


    There are m+n / 4 hexadecimal digits. Did I do the proof correctly ? (except for minor mistakes)
  5. Oct 21, 2009 #4
    That looks pretty good, except I think you need to make it clear that the terms are powers of 16. That will make the notation cumbersome though. For example, [tex]2^{m+n-3}[/tex] would be [tex](2^{4})^{(m+n+1)/4 - 1}[/tex]. Not too pretty, but a cleaner way doesn't come to mind at the moment.
  6. Oct 21, 2009 #5
    Try thinking about it this way:

    Each more significant binary digit is represented by a 0 or 1 multiplied by a power of 2 that is greater than the previous digit by 1. In other words (sticking with whole numbers), the lowest binary digit can be represented by [tex]b_02^0[/tex]

    Remembering, of course that [tex]b_n=0[/tex] or [tex]b_n=1[/tex] for all n.

    The next more significant binary digit can be represented by [tex]b_12^1[/tex]

    and so on... Then, for any binary number with more than 4 digits, you have: [tex]2^n , 2^{n-1}, ..., 2^3, 2^2, 2^1, 2^0[/tex]

    By breaking a large (more than 4 digits) binary number into quartets, and re-assigning them to values of 0..15, you are actually reassigning the appropriate power of 2 (the exponent) for all digits more significant than [tex]b_32^3[/tex]

    Obviously, for the 1st quartet (4 least significant bits), which is [tex]b_3b_2b_1b_0[/tex], the value is already correct (there is no reassignment of exponents) and the quartet's value is [tex]b_32^3+b_22^2+b_12^1+b_02^0[/tex]

    For the 2nd quartet, [tex]b_7b_6b_5b_4[/tex], you are essentially changing the exponents from [tex]2^7, 2^6, 2^5, and \phantom{1}2^4[/tex] to [tex]2^3, 2^2, 2^1, and\phantom{1}2^0[/tex]
    This results in the 2nd quartet having the value [tex]b_72^3+b_62^2+b_52^1+b_42^0[/tex] (The actual value is, of course: [tex]b_72^7+b_62^6+b_52^5+b_42^4[/tex])

    What you've done here is subtracted 4 from each of the exponents in the 2nd quartet: [tex]b_72^{7-4}+b_62^{6-4}+b_52^{5-4}+b_42^{4-4}[/tex]

    Using one of the laws of exponents: [tex]\frac{X^a}{X^b}=X^{a-b}[/tex], we can show that 2nd quartet as: [tex]b_7\frac{2^7}{2^4}+b_6\frac{2^6}{2^4}+b_5\frac{2^5}{2^4}+b_4\frac{2^4}{2^4}[/tex]

    We can then factor out [tex]\frac{1}{2^4}[/tex], resulting in [tex]\frac{1}{2^4}(b_72^7+b_62^6+b_52^5+b_42^4)[/tex]

    And, since [tex]2^4=16[/tex], what you've effectively done, is divided the 2nd quartet by 16!

    Extending this same technique out to the next quartet, you'll find that you will have divided that quartet by [tex]2^8=16^2[/tex]

    Remember also, that each more significant digit in hexidecimal (base 16) is a successively larger power of 16, such that you have
    with [tex]h_n[/tex] = 0...9, A..F
  7. Oct 21, 2009 #6
    So, for each quartet [tex]Q_n \phantom{0} (n\geqq1)[/tex], you obtain the hex digit by dividing the binary value of the quartet by [tex]16^{n-1}[/tex]
  8. Oct 23, 2009 #7
    Thank you for the help all of you.

    So do I need to write it like this:

    16^{(m+n-3)/4)}(b_{m+n}2^3+b_{m+n-1}2^2+...+b_{m+n-3}2^0)+16^{(m+n-3)/4 -1}(b_{m+n-4}2^3+...+b_{m+n-7}2^0)....+16^0(b_32^3+b_22^2+b_12^1+b_02^0)
  9. Oct 23, 2009 #8


    User Avatar
    Staff Emeritus
    Science Advisor

    Of course 100002= 16 so while 01112= (01112)(1)= (01112)(160)= 7(160), the second set, "00112", is (00112)(16)= (00112)(161)= 3(161, the third "10012 is (10012)(162)= 9(162), and the fourth "10002" is (10002)(163)= 7(163.

  10. Oct 24, 2009 #9
    Ok, I believe to makes things better I should use m+n-1 = 4k
  11. Oct 24, 2009 #10
    I would simplify things a little by first stating that any number (binary, or otherwise) can be led with any number of 0's and still retain the same value. That said, I would FIRST add the number of 0's needed (one, two, or three of them) to make the number of binary digits (bits) evenly divisible by 4. Then use "n" to represent the total number of bits in the binary number (including those added 0's, if any).

    Then, you can write it as:

    [tex]16^{\frac{n}{4}-1}(b_n2^3 + b_{n-1}2^2 + b_{n-2}2^1 + b_{n-3}2^0) + 16^{\frac{n}{4}-2}(b_{n-3}2^3 + b_{n-4}2^2 + b_{n-5}2^1 + b_{n-6}2^0) + ... + 16^1(b_72^3 + b_62^2 + b_52^1 + b_42^0) + 16^0(b_32^3 + b_22^2 + b_12^1 + b_02^0)[/tex]
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Binary to hex conversion proof
  1. Dead hex (Replies: 13)