ToBase64

Description

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

Category

Conversion functions

Syntax


ToBase64(string or binary_object) 

See also

See also:

Parameters

Parameter
Description
string or binary_object
String or binary object to convert to Base64.

Usage

Base64 provides 6 bit encoding of 8-bit ASCII characters. Because high ASCII values and binary objects are not safe for transport over internet protocols such as HTTP and SMTP, ColdFusion offers Base64 as a means to safely send ASCII and binary data over these protocols.

Base64 lets you store binary objects in a database if you convert the data into Base64 first.


Note

To reverse the Base64 encoding of a string, you must convert it into a binary object, and then convert the binary object into a string using ToString.


Example

<!--- This example shows the use of ToBinary and ToBase64 --->

<html>

<head>

<title>

ToBase64 Example

</title>

</head>



<BASEFONT FACE = "Arial, Helvetica" SIZE = 2>

<body bgcolor = "#FFFFD5">



<H3>ToBase64 Example</H3>



<!--------------------------------------------------------------------

Initialize data.

---------------------------------------------------------------------->

<cfset charData = "">

<!--------------------------------------------------------------------

Create a string of all ASCII characters (32-255) and 

concatenate them together.

---------------------------------------------------------------------->

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

</body>

</html>




Banner.Novgorod.Ru