Converting Data Between String, Binary, Hex, And Base64 Format In ColdFusion

Posted August 22, 2012 at 10:16 AM by Ben Nadel

Tags: ColdFusion

The other day, when I was looking up test cases for my Crypto.cfc project, I came across some hex-encoded values that I needed to convert to plain "String" values before I could easily use them in my Unit Tests. After doing this, it occurred to me that it might be fun to wrap the ColdFusion functions, binaryEncode() and binaryDecode(), into smaller, one-liner functions for data-type conversion. What I came up with was the following one-liners:

  • base64ToHex()
  • base64ToString()
  • hexToBase64()
  • hexToString()
  • stringToBase64()
  • stringToBinary()
  • stringToHex()

I left out the following data type conversions because they can already be performed in ColdFusion using a single line of code:

  • base64ToBinary() :: binaryDecode( base64Value, "base64" )
  • binaryToBase64() :: binaryEncode( binaryValue, "base64" )
  • binaryToHex() :: binaryEncode( binaryValue, "hex" )
  • binaryToString() :: toString( binaryValue )
  • hexToBinary() :: binaryDecode( hexValue, "hex" )
  • stringToBase64() :: toBase64( stringValue )

I know that my use of the term, "string," is not so semantically correct. After all, my Hex and Base64 values are also strings. In this case, I am simply using the term "string" to denote a value that doesn't represent a hex or base64 binary-encoding.

It might be easier to just look at the functions:

  • <cfscript>
  •  
  •  
  • function base64ToHex( String base64Value ){
  •  
  • var binaryValue = binaryDecode( base64Value, "base64" );
  • var hexValue = binaryEncode( binaryValue, "hex" );
  •  
  • return( lcase( hexValue ) );
  •  
  • }
  •  
  •  
  • function base64ToString( String base64Value ){
  •  
  • var binaryValue = binaryDecode( base64Value, "base64" );
  • var stringValue = toString( binaryValue );
  •  
  • return( stringValue );
  •  
  • }
  •  
  •  
  • function hexToBase64( String hexValue ){
  •  
  • var binaryValue = binaryDecode( hexValue, "hex" );
  • var base64Value = binaryEncode( binaryValue, "base64" );
  •  
  • return( base64Value );
  •  
  • }
  •  
  •  
  • function hexToString( String hexValue ){
  •  
  • var binaryValue = binaryDecode( hexValue, "hex" );
  • var stringValue = toString( binaryValue );
  •  
  • return( stringValue );
  •  
  • }
  •  
  •  
  • function stringToBase64( String stringValue ){
  •  
  • var binaryValue = stringToBinary( stringValue );
  • var base64Value = binaryEncode( binaryValue, "base64" );
  •  
  • return( base64Value );
  •  
  • }
  •  
  •  
  • function stringToBinary( String stringValue ){
  •  
  • var base64Value = toBase64( stringValue );
  • var binaryValue = toBinary( base64Value );
  •  
  • return( binaryValue );
  •  
  • }
  •  
  •  
  • function stringToHex( String stringValue ){
  •  
  • var binaryValue = stringToBinary( stringValue );
  • var hexValue = binaryEncode( binaryValue, "hex" );
  •  
  • return( lcase( hexValue ) );
  •  
  • }
  •  
  •  
  • // ------------------------------------------------------ //
  • // ------------------------------------------------------ //
  • // ------------------------------------------------------ //
  • // ------------------------------------------------------ //
  •  
  •  
  •  
  • // Let's create a string value to test with.
  • message = "Danger zone!";
  •  
  • writeOutput( "Original :: " & message );
  • writeOutput( "<br />" );
  •  
  •  
  • // Now, let's check to the String-to-XXX conversions.
  • writeOutput( "<br />" );
  •  
  •  
  • messageAsHex = stringToHex( message );
  •  
  • writeOutput( "Hex :: " & messageAsHex );
  • writeOutput( "<br />" );
  •  
  • messageAsBase64 = stringToBase64( message );
  •  
  • writeOutput( "Base64 :: " & messageAsBase64 );
  • writeOutput( "<br />" );
  •  
  • messageAsBinary = stringToBinary( message );
  •  
  • writeOutput( "Binary :: [B" & arrayLen( messageAsBinary ) );
  • writeOutput( "<br />" );
  •  
  •  
  • // Now let's try converting back to string.
  • writeOutput( "<br />" );
  •  
  •  
  • hexToMessage = hexToString( messageAsHex );
  •  
  • writeOutput( "From Hex :: " & hexToMessage );
  • writeOutput( "<br />" );
  •  
  • base64ToMessage = base64ToString( messageAsBase64 );
  •  
  • writeOutput( "From Base64 :: " & base64ToMessage );
  • writeOutput( "<br />" );
  •  
  • binaryToMessage = toString( messageAsBinary );
  •  
  • writeOutput( "From Binary :: " & binaryToMessage );
  • writeOutput( "<br />" );
  •  
  •  
  • // Now let's just test the cross-encoding conversions.
  • writeOutput( "<br />" );
  •  
  •  
  • check = stringToHex( message );
  • check = hexToBase64( check );
  • check = base64ToString( check );
  •  
  • writeOutput( "Check 1 :: " & check );
  • writeOutput( "<br />" );
  •  
  • check = stringToBase64( message );
  • check = base64ToHex( check );
  • check = hexToString( check );
  •  
  • writeOutput( "Check 2 :: " & check );
  • writeOutput( "<br />" );
  •  
  •  
  • </cfscript>

As you can see, most of these make use of one or both of the binary encode/decode functions in ColdFusion. When we run the above code, we get the following page output:

Original :: Danger zone!

Hex :: 44616e676572207a6f6e6521
Base64 :: RGFuZ2VyIHpvbmUh
Binary :: [B12

From Hex :: Danger zone!
From Base64 :: Danger zone!
From Binary :: Danger zone!

Check 1 :: Danger zone!
Check 2 :: Danger zone!

Lana? Lana? Laaaaaaannaaaaa! WHAT?!?! Danger zone!!!!!! Anyway, this is pretty minor stuff. But, I happen to come across the need for these data-type of conversions on a fairly regular basis.




Reader Comments

Aug 22, 2012 at 11:53 AM // reply »
18 Comments

Quick pedantic note: as of CF7 Adobe recommends against using toString, toBinary and toBase64; and instead using CharsetEncode, BinaryDecode, and BinaryEncode, respectively. You've got a mixture going on in the code above.

As an aside, what versions of CF to your want Crypto.cfc to work with? I believe it's currently limited to >= 8, due to script only cfc.


Aug 22, 2012 at 12:02 PM // reply »
11,238 Comments

@Grumpy,

Definitely I use toBase64() rather than binaryEncode() out of habit. I would be fine switching over to binaryEncode() for that. However, I am not sure how comfortable I am with the charsetEncode() method. I don't believe I have ever used it; and, the documentation for it looks a bit confusing. I'll have to look into it a bit more to see how it works.

As far as Crypto.cfc, I didn't think much about the compatibility. I built it in CF9, but it should work with CF8 as well. I don't even have any instances of CF7 to test on ; but, CF8 introduced so many syntactic updates that writing things for CF7 compatibility would probably not be worth-while.


Aug 22, 2012 at 5:47 PM // reply »
18 Comments

Something I threw into pastebin last year in response to a Ray Camden blog post uses those functions...

http://pastebin.com/bFb1bBpU

With a bit of luck it's correct and helpful.


Aug 22, 2012 at 6:58 PM // reply »
11,238 Comments

@Grumpy,

Thanks, I'll definitely take a look.


Aug 23, 2012 at 9:26 AM // reply »
11,238 Comments

@Grumpy,

I did it :) I finally looked into it:

http://www.bennadel.com/blog/2415-Converting-Between-String-And-Binary-Values-In-ColdFusion.htm

I'll try to use them, since they are the recommended approaches by Adobe; but, I can't help but think that the older approaches just read better :)


Aug 23, 2012 at 11:18 AM // reply »
18 Comments

@Ben

w00t! Thankfully the new functions are tucked away in helper functions which are even easier to read then the old CF functions. :-)


Aug 23, 2012 at 2:19 PM // reply »
11,238 Comments

@Grumpy,

I think I can be sold on using it :) The one thing that keeps tripping me up is the seeming conflict (in my brain) between the meaning of "encode".

With binaryEncode(), I read it as, "encode this binary value as something else."

This causes me to ready charsetEncode() as "encode this character set as something else."

... but, charsetEncode() actually means just the opposite - "encode something else [binary value] as a character set".

Of course, this probably stems from me completely misunderstanding the meaning of "charset" as it probably has more to do with the range of values rather than my mental model of "string" characters.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 7:46 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
No luck. At least I have uncovered the cause, URLScan 3.1. Here is what I see in the IIS log when a file is over 30mb. 2013-05-21 23:29:05 10.105.45.128 GET /plupload/assets/jquery/jquery-1.8. ... read »
May 21, 2013 at 6:12 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
Ben, I did not see you after Pete Freitag's Lockdown session at cfObjective but he said that IIS sets file size limits at 30MB by default which just happened to be the threshold for file size when ... read »
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools