cfcdev  

Re: [CFCDev] BinaryDecode

Doug Arthur
Thu, 13 Oct 2005 10:51:00 -0700

I happen to have something similar but w/o using java... Just incase this could be of any use to anyone out there...

Note: I have formated this page to fit the scenario. I've taken these functions from an AES Encryption routine I created in a cfc...

cfm code
--------------------------------------

<cffunction name="dump">
 <cfdump var="#Arguments[1]#">
</cffunction>

<cfscript>
 string = "This string should be converted to binary, then to hex, back to binary and then back to a string equally while one method using and another method not using java.";

 function toByteArray(obj) { // can be a string or binary object.
  bytesPerBlock = 128 / 8;
  i = 0;

  if(NOT IsArray(obj)) {
   obj = arraysplit(obj);
   for(i=1;i lte ArrayLen(obj);i=i+1)
    obj[i] = Asc(Left(obj[i], 1));
  }

  return obj;
 }

 function byteArrayToHex(byteArray) {
  var i = 0;
  var result = "";

  if(NOT ArrayLen(byteArray))
   return;
  for(i=1;i lte ArrayLen(byteArray);i=i+1) {
   if(byteArray[i] lt 16) {
    result = result & "0" & FormatBaseN(byteArray[i], 16);
   }
   else {
    result = result & "" & FormatBaseN(byteArray[i], 16);
   }
  }

  return result;
 }

 function hexToByteArray(hexString) {
  var i = 0;
  var j = 0;


  byteArray = ArrayNew(1);
  if(Len(hexString) MOD 2) return byteArray;
  if(Left(hexString, 2) EQ "0x") {
   hexString = Right(hexString, Len(hexString)-2);
   hexString = hexString & "0x"; // pad hexString to it's correct byte size
  }
  for(i=0;i lt Len(hexString);i=i+2) {
   chkString = Mid(hexString, i+1, 2);
   if(Len(chkString)) {
    parsedString = "";
    if(Find("+", chkString) EQ 1) {
     chkString = Replace(chkString, "+", "");
     chkString = chkString & "+";
    }
    for(j=1;j lte Len(chkString);j=j+1) {
     parsedhexString = parseIntBase16(chkString);
     if(Len(parsedhexString))
      byteArray[Fix(i/2)+1] = InputBaseN(parsedhexString, 16);
     else
      byteArray[Fix(i/2)+1] = 0;
    }
   }
  }
  return byteArray;
 }

 function byteArrayToString(byteArray) {
  var result = "";
  var i = 0;

  for(i=1;i lte ArrayLen(byteArray);i=i+1)
   if(byteArray[i] NEQ 0)
    result = result & Chr(byteArray[i]);
  return result;
 }

 function arraysplit(string) {
  var i = 0;
  var arSplit = ArrayNew(1);
  for(i=1;i lte Len(string);i=i+1) {
   arSplit[i] = Mid(string, i, 1);
  }
  return arSplit;
 }


 function parseIntBase16(string) {
  parsedString = "";
  string = Replace(string, "+", "");
  for(z=1;z lte Len(string);z=z+1)
   if(ReFind('^[a-fA-F0-9]', Mid(string, z, 1)))
    parsedString = parsedString & Mid(string, z, 1);
   else
    break;

  return parsedString;
 }

 

 // EXAMPLE OUTPUT
 bin = ToBinary(ToBase64(string));
 byteArray = toByteArray(bin);

 hex = byteArrayToHex(byteArray);
 bin_out = ToBinary(ToBase64(byteArrayToString(byteArray)));
 string_out = byteArrayToString(byteArray);


 WriteOutput("<h2>Results without Java:</h2>");
 dump(bin);
 WriteOutput(hex);
 dump(bin_out);
 WriteOutput(string_out);
</cfscript>

 <cffile action="" file="c:\test\test_ou1t.txt" output="#bin#">

 

 


<br />
<br />
<br />
<br />

 

 

 

<h2>Results with Java:</h2>
<cfscript>
/**
 * Converts binary data to hexadecimal format.
 *
 * @param bin   Binary data. (Required)
 * @return Returns a string. If an error occurs, returns
 * @author Axel Glorieux ([EMAIL PROTECTED])
 * @version 1, June 28, 2002
 */
function Bin2Hex (bin){
 var str64 = tobase64(bin);
 var result = "";
 var a = 0;
 var b = 0;
 var c = "";
 var x = "";
 var i = 1;
 if (isbinary(bin)) {
  for (i;i LTE len(str64);i=i+1){
   c = asc(mid(str64,i,1));
   if (c LT 47)x = 62;
   else if (c LT 48)x = 63;
   else if (c EQ 61)x = 0;
   else if (c LT 65)x = c+4;
   else if (c LT 97)x = c-65;
   else x = c-71;
   if (i mod 2){
    a = bitshln(x,6);
   }
   else {
    b = x;
    for (j=len(formatbasen(a+b,16));j LT 3;j=j+1){
     result = result & 0;
    }
    result = result & formatbasen(a+b,16);
    a = 0;
    b = 0;
    }
   }
   if (right(str64,2) IS "=="){
    result = left(result,len(result)-4);
   }
   else if (right(str64,1) IS "="){
    result = left(result,len(result)-2);
   }
  return result;
 }
 else {
  result = "Invalid binary data";
 }
}
</cfscript>

 

<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>

<cffile action="" file="c:\test\test.txt" variable="bin">
<cfdump var="#bin#">
<cfset hex = Bin2Hex(bin)>
<cfoutput>#hex#</cfoutput>
<cfset bin = Hex2Bin(hex)>
<cfdump var="#bin#">
<cfoutput>#ToString(bin)#</cfoutput>
<cffile action="" file="c:\test\test_out2.txt" output="#bin#">

 

 

On 9/28/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Wow!  Thank You!
>
>
> Jason Cronk
> Verizon
>
>
>
>
>
>                      "Roland Collins"
>                      <[EMAIL PROTECTED]        To:       CFCDev@cfczone.org
>                      .com>                    cc:
>                      Sent by:                 Subject:  RE: [CFCDev] BinaryDecode
>                      [EMAIL PROTECTED]
>                      one.org
>
>
>                      09/28/2005 05:16
>                      PM
>                      Please respond to
>                      CFCDev
>
>
>
>
>
>
> I wrote you a function to take care of it for you since I couldn't find
> one.
> It uses underlying Java streams to get the job done.  It takes in the Hex
> encoded input stream and returns a byteArray, which servers as binary data.
> Here's the function and some example usage:
>
> <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>
>
> <cffile action="" file="c:\test_in.txt" variable="bin">
> <cfset hex = Bin2Hex(bin)>
> <cfoutput>#hex#</cfoutput>
> <cfset bin = Hex2Bin(hex)>
> <cffile action="" file="c:\test_out.txt" output="#bin#">
>
>
> Hope this helps!
> Roland
>
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto: [EMAIL PROTECTED]] On Behalf
> Of [EMAIL PROTECTED]
> Sent: Wednesday, September 28, 2005 1:06 PM
> To: CFCDev@cfczone.org
> Subject: [CFCDev] BinaryDecode
>
>
>
>
>
>
>
>
>
>
>
>  I'm on Cf 6.1 and using bin2hex to convert binary variables to hexadecimal
> equivalent (see
>  http://www.cflib.org/udf.cfm?ID=551). How can I go backwards? 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.1.
> Alternatively, if someone knows a
>  preexisting function to convert hex to base64, then I can use CF function
> tobinary(base64 string).
>
>
>  Any help is appreciated.
>
>
>
>
>
>  Jason Cronk
>
>  Verizon
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email to
> cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of
> the
> email.
>
> CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting
> (www.cfxhosting.com).
>
> CFCDev is supported by New Atlanta, makers of BlueDragon
> http://www.newatlanta.com/products/bluedragon/index.cfm
>
> An archive of the CFCDev list is available at
> www.mail-archive.com/cfcdev@cfczone.org
>
>
>
>
>
>
>
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email to
> cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of
> the email.
>
> CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting
> (www.cfxhosting.com).
>
> CFCDev is supported by New Atlanta, makers of BlueDragon
> http://www.newatlanta.com/products/bluedragon/index.cfm
>
> An archive of the CFCDev list is available at
> www.mail-archive.com/cfcdev@cfczone.org
>
>
>
>
>
>
>
>
>
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email to cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of the email.
>
> CFCDev is run by CFCZone ( www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).
>
> CFCDev is supported by New Atlanta, makers of BlueDragon
> http://www.newatlanta.com/products/bluedragon/index.cfm
>
> An archive of the CFCDev list is available at www.mail-archive.com/cfcdev@cfczone.org
>
>
>
 
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).

CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm

An archive of the CFCDev list is available at www.mail-archive.com/cfcdev@cfczone.org
  • Re: [CFCDev] BinaryDecode Doug Arthur