User Guide

722 Chapter 3: ColdFusion Functions
Base64 lets you store binary objects in a database.
Note: To reverse Base64 encoding of a string, you can convert it to a binary object, then convert the
binary object to a string, using the toString function.
Example
<h3>ToBase64 Example</h3>
<!--- Initialize data. ---->
<cfset charData = "">
<!--- Create string of ASCII characters (32-255); concatenate them --->
<cfloop index = "data" from = "32" to = "255">
<cfset ch = chr(data)>
<cfset charData = charData & ch>
</cfloop>
<p>
The following string is the concatenation of all characters (32 to 255)
from the ASCII table.<br>
<cfoutput>#charData#</cfoutput>
</p>
<!--- Create a Base64 representation of this string. ---->
<cfset data64 = toBase64(charData)>
<!----- Convert string to binary. ------->
<cfset binaryData = toBinary(data64)>
<!--- Convert binary back to Base64. ---->
<cfset another64 = toBase64(binaryData)>
<!---- Compare another64 with data64 to ensure that they are equal. ---->
<cfif another64 eq data64>
<h3>Base64 representations are identical.</h3>
<cfelse>
<h3>Conversion error.</h3>
</cfif>