Skip navigation
Currently Being Moderated

How do you convert hexadecimal to binary?

Apr 12, 2012 10:40 AM

Hello,

 

I am working on a project where I need to convert a hexadecimal packet to binary.  It is looking like I would use the BinaryEncode function, but I wasn't really sure if that was the right way to go about it or not.

 

For instance, I need to convert '01DD6300000C0E1012' to '00000001110111010110001100000000000000000000110000001110000100000001 0010'.

 

I had thought it would be straight forward such.

 

<cfset myHex = "01DD6300000C0E1012">

 

<cfset myBinary = BinaryDecode(myHex, "hex")>

 

<cfoutput>

    <h2>

        myHex = #myHex#<br />

        myBinary = #myBinary#

    </h2>

</cfoutput>

 

However, I end up getting the following error.

 

ByteArray objects cannot be converted to strings.

 

The error occurred in C:\inetpub\wwwroot\playground\hexIT.cfm: line 17

 

15 :     <h2>

16 :         myHex = #myHex#<br />

17 :         myBinary = #myBinary#

18 :     </h2>

19 : </cfoutput>

 

I was expecting that the result was going to be a string of data and not a ByteArray.

 

Is there a better way to go about this?

 

Best regards

KR

 
Replies
  • Currently Being Moderated
    Apr 12, 2012 11:43 AM   in reply to Krandolph

    The set of functions you want is: inputBaseN, formatBaseN. For example,

     

    <cfset myHex                 = "1DD630000C0E1012">

    <cfset myHexInBase10  = inputBaseN(myHex,16)>

    <cfset myHexInBase2    = formatBaseN(myHexInBase10,2)>

     

    <cfoutput>

        <h2>

        myHex = #myHex#<br />

        myBinary = #myHexInBase2#

        </h2>

     

    </cfoutput>

     

    However, I did use a smaller hex. It seems your original number is larger than ColdFusion's range.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 12, 2012 1:33 PM   in reply to Krandolph

    Can you give an example of the expected results?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 12, 2012 3:23 PM   in reply to Krandolph

    So just convert straight from HEX to the 8-bit decimal value?

     

    Well "1DD63" does not convert to 186 directly. It really depends on the packet itself and the decoding instructions you were given. It might be as simple as using input/formatBaseN, or you might need extra code, depending on how many values and what you are ultimately decoding. (That is not clear from the original question). Exactly what were the instructions about the string's composition and extraction of the values?

     

    sums up my feelings on binary.

     

    Lol, you are not alone in that.

     

    Message was edited by: -==cfSearching==-

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 12, 2012 6:23 PM   in reply to Krandolph

    Not to state the obvious, but there is something missing from this equation. Because AFAIK, 1DD63 does not convert to 10111010 (or 186) in binary.  Not even if you break the int into four bytes.

     

    Are those the only instructions they gave you about the string and its format?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2012 12:29 AM   in reply to Krandolph

    Krandolph wrote:

     

    convert the binary value to 8-bit decimal.

    Remember that 8-bit decimal is a compound expression. It contains two concepts: 8-bit and decimal.

     

    In standard notation, it means an integer which is in the interval [0,255] when converted to base 10. For example, 256 is out of the range, and so is no 8-bit number. Unless of course we allow a cyclic rotation modulo 256, whereby 256 reverts to 0, 257 to 1, and so on.

     

    The base 16 number 1DD63 is much larger than 255 when converted to base 10. It is in fact 122211 in base 10, and is therefore not 8-bit. Also, even if you rotate it cyclically modulo 256, you will arrive at 99, which doesn't match any of the numbers you've given.

     

    Starting from other angles, the base 16 number 1DD63 converts to 11101110101100011 in binary. The decimal number 186 converts to BA in hexadecimal, not to 1DD63 as you suggest.

     

    You can already tell, without doing any calculation at all. The bases 2, 10 and 16 are all even. So, if you convert an odd number from any of the bases to another, it must end in an odd integer. Likewise, if you convert an even number from any of the bases to another, it must end in an even integer. This enables you to immediately rule out any even-to-odd or odd-to-even conversions.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 16, 2012 2:56 PM   in reply to Krandolph

    Thanks for the help.  After you said that the number doesn't convert directly over, a coworker rechecked

    the other guys stuff and found that he was over thinking the issue.  The value we wanted was actually directly in the binary.

     

    Good, that makes a lot more sense   :-)

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 17, 2012 12:44 AM   in reply to Krandolph

    Krandolph wrote:

     

    Hello,

     

    Thanks for the help.  After you said that the number doesn't convert directly over, a coworker rechecked the other guys stuff and found that he was over thinking the issue.  The value we wanted was actually directly in the binary.

    Marking this as the answer misinforms the forum. What's this got to do with your original question,"How do you convert hexadecimal to binary?".

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points