www.fusebox.org



Navigation:


FORUMS > Code Warriors
Replying to Topic: Hex to Binary
Created On Thu September 22, 2005 11:39 AM by loddus


Username:
Password:

Remember my login


[ Forget your login information? | Join the forums ]




loddus
Member

Posts: 66
Joined: Sep 2004

Thu September 22, 2005 11:39 AM
User is offline

I'm on Cf 6.5 and am using bin2hex to convert binary variables to hexadecimal equivalent (see http://www.cflib.org/udf.cfm?ID=551). How can I go backwalds? I have a hexadecimal and need the binary value to store into my database. In CF 7 I can use binarydecode but not in 6.5. Alternatively if someone knows a function to convert hex to base64, then I can use CF tobinary(base64 string).


Jason
 
Reply
   
Quote
   
Top
   
Bottom
     



James Agnostic
Senior Member

Posts: 516
Joined: Jun 2003

Thu September 22, 2005 1:09 PM
User is offline

There was a Cold Fusion 6.5?
 
Reply
   
Quote
   
Top
   
Bottom
     



loddus
Member

Posts: 66
Joined: Sep 2004

Thu September 22, 2005 2:05 PM
User is offline

Sorry 6.1..... go too many things on the brain.
 
Reply
   
Quote
   
Top
   
Bottom
     



loddus
Member

Posts: 66
Joined: Sep 2004

Mon October 03, 2005 10:03 AM
User is offline

Roland Collins was kind enough to write a reverse function for me and posted it on the CFC mailing list. I add it here for anybody who may stumble upon this thread. I always hate finding threads about problems I'm having but no indication if a solution was ever found.

<cffunction name="Hex2Bin" returntype="any" hint="Converts a Hex string to
binary">
<cfargument name="inputString" type="string" required="true"
hint="The hexadecimal string to be written.">

<cfset var outStream = CreateObject("java",
"java.io.ByteArrayOutputStream").init()>
<cfset var inputLength = Len(arguments.inputString)>
<cfset var outputString = "">
<cfset var i = 0>
<cfset var ch = "">

<cfif inputLength mod 2 neq 0>
<cfset arguments.inputString = "0" & inputString>
</cfif>

<cfloop from="1" to="#inputLength#" index="i" step="2">
<cfset ch = Mid(inputString, i, 2)>
<cfset outStream.write(javacast("int", InputBaseN(ch, 16)))>
</cfloop>

<cfset outStream.flush()>
<cfset outStream.close()>

<cfreturn outStream.toByteArray()>
</cffunction>
 
Reply
   
Quote
   
Top
   
Bottom
     



serious
Member

Posts: 43
Joined: Jun 2005

Tue October 04, 2005 1:50 AM
User is offline View users profile

You could use soemthing this simple:
var bin = FormatBaseN(InputBaseN(getHex(byteArray, offSet),16),2);
bin = RepeatString("0", (8-Len(bin))) & bin;

Or if you are wanting to convert more than one byte I use the following, although it is reading from a byte array, I added the code for the byte array as well.


function getByteArray(size){
     var byteClass = createObject("java", "java.lang.Byte").TYPE;
     var byteArray = createObject("java","java.lang.reflect.Array").newInstance(byteClass, arguments.size);
     return byteArray;
}

function getBinary(byteArray, offSet){
     var hex = BitAnd(byteArray[offSet],255);

// optional arguments at the end are [3] byte count and [4] byte order

     var bigEndian = (ArrayLen(arguments) neq 4 OR arguments[4]);
     var byteCount = 1;
     var bin = FormatBaseN(InputBaseN(getHex(byteArray, offSet),16),2);
     bin = RepeatString("0", (8-Len(bin))) & bin;
     if(ArrayLen(arguments) gte 3 AND IsNumeric(arguments[3]) AND arguments[3] gte 2){
          byteCount = arguments[3];
          for(ind=1; ind lt byteCount; ind=ind+1){
               binPart = FormatBaseN(InputBaseN(getHex(byteArray, offSet+ind),16),2);
               binPart = RepeatString("0", (8-Len(binPart))) & binPart;
               bin = IIF(bigEndian, bin & binPart, binPart & bin);
          }
     }
     len = byteCount*8;
     bin = RepeatString("0", (len-Len(bin))) & bin;
     return bin;


-------------------------
Marcel
http://cf.iamserious.com.au
cf_img
cf_imgsize

Edited: Tue October 04, 2005 at 1:51 AM by serious

 
Reply
   
Quote
   
Top
   
Bottom
     



jfill
Junior Member

Posts: 12
Joined: Nov 2002

Fri October 28, 2005 6:03 PM
User is offline

serious,

Where does getHex() come from? I am getting errors that getHex is undefined.

jfill
 
Reply
   
Quote
   
Top
   
Bottom
     



serious
Member

Posts: 43
Joined: Jun 2005

Sun November 06, 2005 10:58 PM
User is offline View users profile

Sorry left it out.


     function getHex(byteArray, offSet){
          var bin = BitAnd(byteArray[offSet],255);
          var bigEndian = (ArrayLen(arguments) neq 4 OR arguments[4]);
          var byteCount = 1;
          if(ArrayLen(arguments) gte 3 AND IsNumeric(arguments[3]) AND arguments[3] gte 2){
               byteCount = arguments[3];
               byte1 = IIF(bigEndian, 8, 0);
               byte2 = IIF(bigEndian, 0, 8);
               for(ind=1; ind lt byteCount; ind=ind+1){
                    bin = BitOr(BitSHLN(BitAnd(byteArray[offSet+ind],255),byte2),
                                     BitSHLN(bin,byte1));
               }
          }
          len = byteCount*2;
          hex = FormatBaseN(bin,16);
          hex = RepeatString("0", (len-Len(hex))) & hex;
          return hex;
     }


-------------------------
Marcel
http://cf.iamserious.com.au
cf_img
cf_imgsize

 
Reply
   
Quote
   
Top
   
Bottom
     



serious
Member

Posts: 43
Joined: Jun 2005

Sun November 06, 2005 11:01 PM
User is offline View users profile

They're not perfect but I posted them so you could get the idea of what it is doing.. it is possible to convert values of any type just takes the patience to figure it out. :)

-------------------------
Marcel
http://cf.iamserious.com.au
cf_img
cf_imgsize
 
Reply
   
Quote
   
Top
   
Bottom
     



jbliss1
Member

Posts: 67
Joined: Jul 2008

Wed September 03, 2008 5:10 PM
User is offline

This thread was extremely helpful today! Now I can go from hex to binary object. In my app, I'm then taking that binary object and passing it to ToBase64 and that works great too. Now my problem is: how to take the resulting Base64 and work my way back to the original hex. Any help would be greatly appreciated!
 
Reply
   
Quote
   
Top
   
Bottom
     

FORUMS > Code Warriors

Navigation:

FuseTalk 4.0 © 1999-2003 FuseTalk Inc.