简体   繁体   中英

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?

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 binaryDecode in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM