BinaryDecode

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 (2021 release): Introduced encoding base64Url

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.
  • Base64Url: modification of the main Base64 standard, which uses the encoding result as filename or URL address.

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

Example 2

<cfscript> 
    longString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@##$%^&*()_+=-{}[]|\:;""'<>?,./"; 
    binaryString = toBinary(toBase64(longString)); 
    //encode binary data 
    encodedBinaryData = binaryEncode(binaryString, "Base64Url");        
    writedump(encodedBinaryData); 
    //decode the encoded binary data 
    decodedBinaryData = binaryDecode(encodedBinaryData, "Base64Url");   
    //verify if the decoded binary data is the same as the source binary data 
    writeOutput("<br/>") 
    if(toString(binaryString) eq toString(decodedBinaryData)) 
    { 
        writeoutput("binaryEncode/binaryDecode of a long string is OK"); 
    } 
    else 
    { 
        writeoutput("binaryEncode/binaryDecode of a long string is NOT OK"); 
    } 
</cfscript>

Output

YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWjEyMzQ1Njc4OTBgfiFAIyQlXiYqKClfKz0te31bXXxcOjsiJzw-PywuLw
binaryEncode/binaryDecode of a long string is OK

 Adobe

Get help faster and easier

New user?