View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > ToBase64 PreviousNext

ToBase64

Calculates the Base64 representation of a string or binary object. The Base64 format uses printable characters, allowing binary data to be sent in forms and e-mail, and stored in a database or file.

The Base64 representation of a string or binary object.

Conversion functions, Other functions, String functions

ToBase64(string or binary_object[, encoding])

ColdFusion MX: Added the encoding attribute.

Parameter

Description

string or binary_object

A string, the name of a string, or a binary object.

encoding

For a string, defines how characters are represented in a byte array. The following list includes commonly used values:

  • utf-8
  • iso-8859-1
  • windows-1252
  • us-ascii
  • shift_jis
  • iso-2022-jp
  • euc-jp
  • euc-kr
  • big5
  • euc-cn
  • utf-16

For more information on character encoding, see:
www.w3.org/International/O-charset.html.

Default: the encoding of the page on which the function is called. See cfcontent.

For a binary object, this parameter is ignored.

Base64 provides 6-bit encoding of data. The encoding process represents 24-bit groups of input bits as output strings of 4 encoded ASCII characters. Binary objects and, in some cases, 8-bit characters, cannot be transported over many internet protocols, such as HTTP and SMTP. Using Base64 safely converts the data into a format that is safe over any internet protocol.

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.

<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>

Contents > CFML Reference > ColdFusion Functions > ToBase64 PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


mkeller said on Dec 28, 2004 at 11:31 AM :
bug in mx seems to add an extraneous character to strings you run toBase64 on so that when you run toBinary on them they do not convert properly and do not appear to be base64 strings. To fix this you need to put the trim function around your string when you use toBinary

ie
<cfset base64version = #tobase64(myimage)#>
<cfset binaryversion = #tobinary(trim(base64version))#>

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/funca108.htm