Converting A Base64 Value Back Into A String Using ColdFusion
Posted September 5, 2008 at 2:54 PM
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 | Permalink | Other Searches | Print Page
Newer Post
XML Building / Parsing / Traversing Speed In ColdFusion
Older Post
IMPORTANT UPDATE: XML Parsing Is WAY Faster Than ColdFusion Custom Tags
Reader 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.
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!
Just FYI Ben. The CFML Reference manual is your friend. It's on page 1228.
@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 :)
@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
@John,
Ahh, yes, that makes sense. Thanks for the clarification.




