espresso_coffee
espresso_coffee

Reputation: 6110

Converting binary value to string and back to binary?

I have rowversion value that comes from Database in binary format. I need to convert that value to string in order to pass in my front-end code. Then when user submits data back to the server I need to convert that string back to binary. Here is example of my data:

Binary 00000010586 

Above output is what I see when my query result returns value. Then I tried this:

Encoded value looks like this: iV

Then I tried to decode value back and this is what I have used:

#charsetDecode( local.encodedVal, "utf-8" )#

Then I got this error message: ByteArray objects cannot be converted to strings.

In my database row version column has timestamp type. When query returns that value it's represented as binary in ColdFusion. I use this column as unique id for each row set in my table. Is there a way to handle this conversion in CldFusion adn what would be the best approach?

Upvotes: 1

Views: 1342

Answers (1)

Twillen
Twillen

Reputation: 1466

Your working with binary data, and not with string encodings. You will need to use binaryEncoded and binaryDecode to send your data as a string and convert it back to binary.

The following example converts some binary data into 2 string representations and converts them back into the same byte array with binaryDecodein the dump.

<cfscript>
    binaryData = toBinary("SomeBinaryData++");
    hexEncoded = binaryEncode(binaryData, "hex");
    base64Encoded = binaryEncode(binaryData, "base64");

    writeDump([
        binaryData,
        hexEncoded,
        base64Encoded,
        binaryDecode(hexEncoded, "hex"),
        binaryDecode(base64Encoded, "base64")
    ]);
</cfscript>

Run the example on TryCF.com

Upvotes: 3

Related Questions