View comments | RSS feed

ToBase64

Description

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.

Return value

The Base64 representation of a string or binary object.

Category

Conversion functions, Other functions, String functions

Syntax

ToBase64(string or binary_object[, encoding])

See also

History

New in ColdFusion MX: the encoding attribute is new.

Parameters

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:
  • US-ASCII
  • ISO-8859-1
  • UTF-8
  • UTF-16
Default: the encoding option of the page on which the function is called. See cfcontent.
The Java platform determines the available options.
For a binary object, this parameter is ignored.

Usage

Base64 provides -bit encoding of 8-bit ASCII characters. High ASCII values and binary objects are not safe for transport over internet protocols such as HTTP and SMTP; using Base64 safely sends ASCII and binary data over these protocols.

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>

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

Version 6

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

Comments


ctina said on Jul 15, 2002 at 6:41 PM :
Update from Macromedia:
In the ToBase64 function entry, Usage section, the first sentence should read,
"Base64 provides 6-bit encoding of 8-bit ASCII characters."

 

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

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt2106.htm