Converting Between String And Binary Values In ColdFusion

Posted August 23, 2012 at 9:17 AM by Ben Nadel

Tags: ColdFusion

For years, I have been using the toString(), toBase64(), and toBinary() functions to convert values back and forth between String and Binary formats in ColdFusion. According to the documentation, however, these methods are mostly deprecated. A few people have told me to use the charsetEncode() and charsetDecode() functions instead. And, for years, I ignored this advice. But yesterday, and the behest of Grumpy CFer, I finally decided to look into it.

Before we look at charsetEncode() and charsetDecode(), let's quickly take a look at how I currently convert string values to binary, and vice-versa:

  • <cfscript>
  •  
  •  
  • // These two functions represent the "deprecated" approach to
  • // converting string values back and forth between binary
  • // (according to the ColdFusion documentation).
  •  
  •  
  • function stringToBinary( String stringValue ){
  •  
  • var base64Value = toBase64( stringValue );
  • var binaryValue = toBinary( base64Value );
  •  
  • return( binaryValue );
  •  
  • }
  •  
  •  
  • function binaryToString( Any binaryValue ){
  •  
  • var stringValue = toString( binaryValue );
  •  
  • return( stringValue );
  •  
  • }
  •  
  •  
  • // ------------------------------------------------------ //
  • // ------------------------------------------------------ //
  • // ------------------------------------------------------ //
  •  
  •  
  • // Create a message to convert.
  • message = "Donkeys!";
  •  
  • // Try a string-to-binary-to-string conversion.
  • messageAsBinary = stringToBinary( message );
  • messageAsString = binaryToString( messageAsBinary );
  •  
  • writeOutput( "Original: " & message );
  • writeOutput( "<br />" );
  • writeOutput( "Binary: " & "[B" & arrayLen( messageAsBinary ) );
  • writeOutput( "<br />" );
  • writeOutput( "Converted: " & messageAsString );
  •  
  •  
  • </cfscript>

As you can see, I use the toString(), toBase64(), and toBinary() functions. And, when I run the above code, I get the following output:

Original: Donkeys!
Binary: [B8
Converted: Donkeys!

Excellent! And, this has been working for years.

Ok, now let's take a look at the charsetEncode() and charsetDecode() functions. These two functions convert a value into String format and into Binary format, respectively. Below is the same code as above, only using these newer, recommended functions:

  • <cfscript>
  •  
  •  
  • // These two functions represent the "recommended" way of
  • // converting string values back and forth from binary (according
  • // to the ColdFusion documentation since ColdFusion MX 7).
  •  
  •  
  • function stringToBinary( String stringValue ){
  •  
  • var binaryValue = charsetDecode( stringValue, "utf-8" );
  •  
  • return( binaryValue );
  •  
  • }
  •  
  •  
  • function binaryToString( Any binaryValue ){
  •  
  • var stringValue = charsetEncode( binaryValue, "utf-8" );
  •  
  • return( stringValue );
  •  
  • }
  •  
  •  
  • // ------------------------------------------------------ //
  • // ------------------------------------------------------ //
  • // ------------------------------------------------------ //
  •  
  •  
  • // Create a message to convert.
  • message = "Donkeys!";
  •  
  • // Try a string-to-binar-to-string conversion.
  • messageAsBinary = stringToBinary( message );
  • messageAsString = binaryToString( messageAsBinary );
  •  
  • writeOutput( "Original: " & message );
  • writeOutput( "<br />" );
  • writeOutput( "Binary: " & "[B" & arrayLen( messageAsBinary ) );
  • writeOutput( "<br />" );
  • writeOutput( "Converted: " & messageAsString );
  •  
  •  
  • </cfscript>

When we run this code, we get the same exact output:

Original: Donkeys!
Binary: [B8
Converted: Donkeys!

If Adobe is recommending these functions, I'll start using them as I assume they are recommended for a performance reason. However, the older functions appear to be more straightforward. Sure, I had to use an intermediary toBase64() call; but, the old methods read very well and remove the noise of having to choose a character encoding (does anyone not use UTF-8?).




Reader Comments

Aug 23, 2012 at 3:18 PM // reply »
9 Comments

Adobe might recommend using charsetDecode/charsetEncode especially because of the character encoding. This is only speculation of course.


Aug 23, 2012 at 4:56 PM // reply »
11,241 Comments

@Guillaume,

I assume so as well; but, I wonder what the charset was assumed to be in the earlier versions where it could not be defined??


Aug 24, 2012 at 12:15 PM // reply »
3 Comments

Not using UTF-8 is very common. Notably, Java uses UTF-16 for their internal string representation. UTF-8 has only become popular (relatively) recently, due to the fact that it's partly backwards compatible with ASCII.

I wouldn't be terribly surprised if the default for toString was ASCII (an easy way to test would be to pass a unicode symbol through, and if it gets mangled). It might also do some checking (UTF-16 is easy to recognize because it starts with a special symbol called the byte-order-mark, aka BOM. UTF-8 isn't as easy, but some character values are disallowed in it, so you can rule it out some of the time) before falling back on ASCII.

If you haven't read it already, I recommend Joel Spolsky's blog post about what every developer should know about Unicode/Character encodings (here http://www.joelonsoftware.com/articles/Unicode.html). It explains enough of the details about it (including why specifying them is necessary) to make sense, without going overboard.


Aug 29, 2012 at 11:20 AM // reply »
46 Comments

This is especially useful knowledge. I'm thinking of the struggles with strings to binary, particularly when dealing with DB2 and EBCDIC.


Sep 5, 2012 at 2:52 PM // reply »
1 Comments

Hi Ben,

In your ColdFusion apps when do you find yourself converting from string to binary and vice versa? I have seen it used for representing a XML file as a string and attaching files in an email. Good post (as always), but I feel like I could use more knowledge about potential applications for this in ColdFusion.


Sep 6, 2012 at 9:53 AM // reply »
11,241 Comments

@Thom,

In ColdFusion, strings *are* Java strings, and they seem to be convertible based on a UTF-8 encoding assumption. When you refer to Java's internal string representation, are you referring to the strings we use? Or something done behind the scenes?

I'll try to do some more experimentation and I'll read that Spolsky article, thanks!

@Brian,

Awesome - glad this will help!

@Noah,

No problem - I typically use string-to-binary conversion when streaming values back to the client with the CFContent tag:

  • <cfcontent
  • type="application/json"
  • variable="#stringToBinary( .... )#"
  • />

By using the "variable" attribute, the response closes with the value being streamed. Basically, this resets the output buffer and makes sure that ONLY the given response value (the string being encoded) shows up in the response stream.


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 22, 2013 at 7:52 AM
Nested Views, Routing, And Deep Linking With AngularJS
Hi, Just a quick thank you. As it happens, for my own purposes, the pending ui-router work being done in native angular is likely the one I'll adopt, but your exploration, code and documentation of ... read »
May 22, 2013 at 4:43 AM
How Do You Use The ColdFusion CFParam Tag?
'<cfparam>' or 'isDefined()and <cfset>' performs the same task.Is there any difference? ... read »
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 »
InVision App - Prototyping Made Beautiful With Prototyping Tools