简体   繁体   English

将二进制值转换为字符串并返回二进制?

[英]Converting binary value to string and back to binary?

I have rowversion value that comes from Database in binary format. 我有来自二进制格式的数据库的rowversion值。 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 编码值如下所示: 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. 然后我收到此错误消息: ByteArray objects cannot be converted to strings.

In my database row version column has timestamp type. 在我的数据库中,行版本列具有timestamp类型。 When query returns that value it's represented as binary in ColdFusion. 当查询返回该值时,它在ColdFusion中表示为二进制。 I use this column as unique id for each row set in my table. 我将此列用作表中每行设置的唯一ID。 Is there a way to handle this conversion in CldFusion adn what would be the best approach? 有没有办法在CldFusion中处理这种转换?哪种方法最好?

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. 您将需要使用binaryEncodedbinaryDecode将数据作为字符串发送并将其转换回二进制。

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. 以下示例将一些二进制数据转换为2个字符串表示形式,并将它们转换回转储中带有binaryDecode的相同字节数组。

<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 TryCF.com上运行示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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