Converting A Base64 Value Back Into A String Using ColdFusion

Posted September 5, 2008 at 2:54 PM

Tags: ColdFusion

This might seem super obvious to some of you, but this small problem had me stumped for a good 10 minutes this morning. I had an XML value that I was converting to Base64 for an HTTP post. Then, on the other side, I needed to take that Base64 encoded value and convert it back to a regular string for use with XML parsing. And then it hit, I had no idea how to, in ColdFusion, take a Base64 encoded value and convert it back to the original string.

Once I figured it out, it was laughably simple:

 Launch code in new window » Download code as text file »

  • <!--- Create a string value. --->
  • <cfset strValue = "Hey there cutie patootie." />
  •  
  • <!--- Convert to base 64. --->
  • <cfset strBase64Value = ToBase64( strValue ) />
  •  
  • <!---
  • To convert the base 64 value back to string, simply convert
  • the it a binary representation and then back into to a string.
  • --->
  • <cfset strNewValue = ToString( ToBinary( strBase64Value ) ) />
  •  
  • <!--- Output test data. --->
  • <p>
  • <cfoutput>
  • Base 64: #strBase64Value#<br />
  • Value: #strNewValue#
  • </cfoutput>
  • </p>

Running the above code, we get the following output:

Base 64: SGV5IHRoZXJlIGN1dGllIHBhdG9vdGllLg==
Value: Hey there cutie patootie.

In ColdFusion, the ToBinary() function takes a Base64 encoded value and converts it to its binary representation. This, of course, is a Byte Array. In our example, where each character of a string is represented by a single byte, we have one character per byte array index. Then, calling ToString() on that Byte Array simply converts that character-based byte array back into its string representation.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Sep 5, 2008 at 3:11 PM // reply »
12 Comments

I've done something similar in a MS SQL Server database - take a text field and output as string:

CAST(CAST(textField AS VARBINARY(1000)) AS VARCHAR(1000))

I think that's how it worked. I'm fairly sure most DBMSs have similar functions.


Sep 5, 2008 at 3:42 PM // reply »
3 Comments

Earlier this week, I had to do the same thing except that the strValue was a GUID (hex). I found that converting the GUID to decimal and then trying to run ToBase64 on the resulting HUGE number resulted in a CF error. Apparently ToBase64 can only handle decimals up to a certain size. I did find the solution though (with Ray's help) and posted it here: http://www.coldfusionjedi.com/forums/messages.cfm?threadid=2E1AFB13-19B9-E658-9DEC415EB6931B97

Enjoy!


Sep 5, 2008 at 4:04 PM // reply »
6 Comments

Just FYI Ben. The CFML Reference manual is your friend. It's on page 1228.


Sep 5, 2008 at 4:24 PM // reply »
7,207 Comments

@Matt,

Good to know.

@John,

I assume you needed to go to a decimal first for some reason; otherwise, you can convert any string to Base64 regardless of length.

@Gary,

It's funny, I have gone from String -> Base64 -> Binary so many times that I totally blanked on going the reverse order.

ps. I love the CFML Reference guide :)


Sep 5, 2008 at 4:52 PM // reply »
3 Comments

@Ben,

Yes. Per my post on coldfusionjedi.com, I needed a *shorter* version of the GUID. If you treat it as a (hex) number, the resulting Base64 has fewer characters/digits than the original:

GUID: e1c4c2926-b394-6808-e611-f95a4c41cbf
Base64: 4cTCkms5RoCOYR+VpMQcvw==

If you treat the GUID as a string, the resulting Base64 has more characters/digits than the original:

GUID: e1c4c2926-b394-6808-e611-f95a4c41cbf
Base64: ZTFjNGMyOTI2YjM5NDY4MDhlNjExZjk1YTRjNDFjYmY=

Make sense?

I discovered this phenomenon on Wikipedia, of all places, "When printing fewer characters is desired, GUIDs are sometimes encoded into a base64 or Ascii85 string. Base64-encoded GUID consists of 22 to 24 characters (depending on padding)."

http://en.wikipedia.org/wiki/GUID


Sep 5, 2008 at 4:56 PM // reply »
7,207 Comments

@John,

Ahh, yes, that makes sense. Thanks for the clarification.


Post Comment  |  Ask Ben

Recent Blog Comments
Feb 9, 2010 at 4:14 AM
Ask Ben: Converting a Query to an Array
Exellent script! Tried to make it shorter, gained approx 10% performance improvement :) [CODE] <cffunction name="queryToArray" access="public" returntype="array" output="false"hint="This turns a q ... read »
Feb 9, 2010 at 3:16 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
Just a quick fix for the code example above: $('.panel').height() should instead be... $('.panel').outerHeight() ... read »
Feb 9, 2010 at 3:09 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
I came across this page because I was searching for a certain behavior. I didn't just want the element to slide up; I wanted it's contents to slide up with it. As if the contents were written on a p ... read »
Feb 9, 2010 at 12:57 AM
Ask Ben: Creating A PDF And Attaching It To An Email Using ColdFusion
Have you got any reference to code explaining the procedure to use cfdocument, file to disk and attach to email? ... read »
Feb 8, 2010 at 11:27 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Randi, While I can appreciate your specific situation, I personally am not a fan of a situation where people have the option to run their own ad-hoc reports on the database. This is, honestly, a r ... read »
Feb 8, 2010 at 11:15 PM
Creating Microsoft Excel Documents With ColdFusion And XML
@Candice, No problem - glad you got it figured out. @David, To view the XLS document as an XML file, if I remember correctly, I actually opened up the document in Excel and then went "File > ... read »
Feb 8, 2010 at 11:11 PM
Converting An IP Address To An Integer Using MySQL (Thanks Julian Halliwell)
@Rob, As @Julian pointed out, you need to enable multiple queries in order to run queries containing semi-colons. For more info on that, take a look at this post: http://www.bennadel.com/blog/120 ... read »
Feb 8, 2010 at 11:08 PM
Muscle: Confessions Of An Unlikely Bodybuilder By Samuel Wilson Fussell
@Robert, Awesome. When you start reading it, I'd love for you to drop by and share your comments. I happen to love the book and am always happy to have a good conversation about it. ... read »