Description

Converts a string to a binary object. Used to convert binary data that has been encoded into string format back into binary data.

Returns

A binary object.

Category

Conversion functionsString functions

Function syntax

BinaryDecode(string, encoding)

See also

BinaryEncodeCharsetEncodeCharsetDecode

History

ColdFusion (2018 release): INtroduced named parameters.

ColdFusion MX 7: Added this function.

Parameters

Parameter

Description

string

A string containing encoded binary data.

encoding

A string that specifies the algorithm used to encode the original binary data into a string; must be one of the following:

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

Usage

Use this function to convert a binary-encoded string representation of binary data back to a binary object for use in your application. Binary data is often encoded as a string for transmission over many Internet protocols, such as HTTP and SMTP, or for storage in a database.Adobe recommends that you use the BinaryDecode function, not the ToBinary(base64data) function, to convert Base64-encoded data to binary data in all new applications.See the following pages for additional information on handling binary data:

  • cffile for loading and reading binary data in files
  • 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