ToBinary

Description

Calculates the binary representation of Base64-encoded data or of a PDF document.

Returns

A binary representation of the data.

Function syntax

ToBinary(Data)

See also

  • BinaryDecode for conversion of binary-encoded data, including Base64, to binary data

  • cffile for information about loading and reading binary data

  • cfwddxfor information about serializing and deserializing binary data

  • IsBinary and ToBase64 for checking format and converting to Base64

  • Len for determining the length of a binary object

  • Binary data type and binary encoding in the Developing ColdFusion Applications

Parameters

Parameter

Description

Data

A variable representing data in Base64-encoded format or a PDF document.

Usage

The ToBinary function can take as a parameter a PDF document variable (specified by the cfpdf tag name attribute). In this case, the ToBinary function returns a byte array (byte[]) representation of the document. You can use the results of this function, for example, to store the PDF in a database as a BLOB, or, in a cfcontent tag, to write the PDF to the browser. You can use this binary representation with a read operation in the cfpdf tag to create a variable.

The following example reads an unprotected PDF file, applies protections, and displays it in the browser:

<cfpdf action="read" source="Copy of coldfusion11.pdf" name="p"> 
<cfpdf action="protect" source="p" newUserpassword="user" permissions="none" 
        newOwnerpassword="owner"> 
<cfcontent type="application/pdf" variable="#tobinary(p)#">

Adobe recommends that you use the BinaryDecode function to convert Base64 encoded data to binary data in all new applications.

If you pass a binary value to this function, it returns the input value.

Example

<h3>ToBinary Example</h3> 
<!---- Initialize data. ----> 
<cfset charData = ""> 
<!---- Create a 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 representation of binary data is identical to the Base64 
    representation of string data.</h3> 
<cfelse> 
    <h3>Conversion error.</h3> 
</cfif>