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,246 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,246 Comments

@Grumpy,

Thanks, I'll definitely take a look.


Aug 23, 2012 at 9:26 AM // reply »
11,246 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,246 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 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
May 23, 2013 at 5:19 AM
Ask Ben: Print Part Of A Web Page With jQuery
How to print also the background color of table cells and table lines ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools