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
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.
Hello,
Thanks. That helped tremendously. The good news is that it came to my attention after your reply, that we really only need to parse part of the hex value, so this works out fine. The other news is that it looks like I then have to convert the binary value to 8-bit decimal.
How would I go about that part?
Best regards,
KR
Hello,
The part of the hex I need (1DD63), converts to 10111010 in binary. That in turn converts to 186 in 8-bit decimal. I don't know have a clue what ColdFusion function to use to go from the binary string to the 8-bit decimal.
Now that I know that the final value I need is a 8-bit decimal value, does it make more sense to just convert from 1DD63 directly to 186? So just convert straight from HEX to the 8-bit decimal value?
This is all way out of my normal depth, so any help is appreciated.
Best regards,
KR
01001001 00100000 01101000 01100001 01110100 01100101 00100000 01100010 01101001 01101110 01100001 01110010 01111001 0100001 sums up my feelings on binary.
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==-
Hello,
The starting hexadecimal packet is '01DD6300000C0E1012'. The part that I contains the data I need is in the first batch of characters. The instructions I was given was that the first 6 characters (less the first character) contained the part that I want.
So, my code is now
<cfset myBaseHex = "01DD6300000C0E1012">
<cfset myHex = mid("01DD6300000C0E1012",2,5)>
<cfset myHexInBase10 = inputBaseN(myHex,16)>
<cfset myHexInBase2 = formatBaseN(myHexInBase10,2)>
<cfoutput>
<h2>
myHex = #myHex#<br />
myBinary = #myHexInBase2#
</h2>
</cfoutput>
The next step was to convert the binary part to 8-bit decimal. The person that gave me the data, said that this particular one should resolve to 186.
Best regards,
KR
01001001 00100000 01101000 01100001 01110100 01100101 00100000 01100010 01101001 01101110 01100001 01110010 01111001 0100001 sums up my feelings on binary. I'm glad that at least one person found some humor in my binary :-)
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.
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?".