Description

Converts binary data to a string.

Returns

An encoded string representing the binary data.

Category

Conversion functionsString functions

Function syntax

BinaryEncode(binarydata, encoding)

See also

BinaryDecodeCharsetEncodeCharsetDecode

History

ColdFusion MX 7: Added this function.

Parameters

Parameter

Description

binarydata

A variable containing the binary data to encode.

encoding

A string that specifies the encoding method to use to represent the data; one of the following:

  • Hex: use the characters 0-9 and A-F to represent the hexadecimal value of each byte; for example, 3A.
  • UU: use the UNIX UUencode algorithm to convert the data.
  • Base64: use the Base64 algorithm to convert the data, as specified by IETF RFC 2045, at www.ietf.org/rfc/rfc2045.txt.

Usage

Binary objects and, in some cases, 8-bit characters, cannot be transported over many Internet protocols, such as HTTP and SMTP, and might not be supported by some database systems. By Binary encoding the data, you convert the data into a format that you can transfer over any Internet protocol or store in a database as character data. To convert the data back to a binary format, use the BinaryDecode function. Adobe recommends that you use the BinaryEncode function, and not the ToBase64(binarydata) function, to convert binary data to Base64 data in all new applications.This function provides a superset of the functionality of the ToBase64(binarydata) function.See the following pages for additional information on handling binary data:

  • cffile for loading and reading binary data
  • cfwddx for serializing and deserializing binary data
  • IsBinary for checking variables for binary format
  • Len for determining the length of a binary object

Example

<cfscript>

function base64ToHex( String base64Value ){
        var binaryValue = binaryDecode( base64Value, "base64" );
        var hexValue = binaryEncode( binaryValue, "hex" );
        return( lcase( hexValue ) );
    }
    function base64ToString( String base64Value ){
        var binaryValue = binaryDecode( base64Value, "base64" );
        var stringValue = toString( binaryValue );
        return( stringValue );
    }
    function hexToBase64( String hexValue ){
        var binaryValue = binaryDecode( hexValue, "hex" );
        var base64Value = binaryEncode( binaryValue, "base64" );
        return( base64Value );
    }
    function hexToString( String hexValue ){
        var binaryValue = binaryDecode( hexValue, "hex" );
        var stringValue = toString( binaryValue );
        return( stringValue );
    }
    function stringToBase64( String stringValue ){
        var binaryValue = stringToBinary( stringValue );
        var base64Value = binaryEncode( binaryValue, "base64" );
        return( base64Value );
    }
    function stringToBinary( String stringValue ){
        var base64Value = toBase64( stringValue );
        var binaryValue = toBinary( base64Value );
        return( binaryValue );
    }
    function stringToHex( String stringValue ){
        var binaryValue = stringToBinary( stringValue );
        var hexValue = binaryEncode( binaryValue, "hex" );
        return( lcase( hexValue ) );
    }
    // ------------------------------------------------------ //
    // ------------------------------------------------------ //
    // ------------------------------------------------------ //
    // ------------------------------------------------------ //
    // Let's create a string value to test with.
    message = "GoodMorning! What's Up?";
    writeOutput( "Original :: " & message );
    writeOutput( "<br />" );
    // Now, let's check to the String-to-XXX conversions.
    writeOutput( "<br />" );
    messageAsHex = stringToHex( message );
    writeOutput( "Hex :: " & messageAsHex );
    writeOutput( "<br />" );
    messageAsBase64 = stringToBase64( message );
    writeOutput( "Base64 :: " & messageAsBase64 );
    writeOutput( "<br />" );
    messageAsBinary = stringToBinary( message );
    writeOutput( "Binary :: B" & arrayLen( messageAsBinary ) );
    writeOutput( "<br />" );
    
</cfscript>

Output

Original :: GoodMorning! What's Up?

Hex :: 476f6f644d6f726e696e6721205768617427732055703f
Base64 :: R29vZE1vcm5pbmchIFdoYXQncyBVcD8=
Binary :: B23

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy