简体   繁体   English

将BIT DATA转换为String Java

[英]Convert BIT DATA To String Java

I have a variable OBJECT_ID stored in DB2 as OBJECT_ID VARCHAR() FOR BIT DATA NOT NULL . 我在DB2中存储了一个变量OBJECT_ID作为OBJECT_ID VARCHAR() FOR BIT DATA NOT NULL

How can I retrieve the String value stored in it? 如何检索存储在其中的String值?

I have tried following code to convert Binary to String in Java as: 我尝试了以下代码将Java中的Binary转换为String

String str = resultset.getObject("OBJECT_ID").toString();
int charCode = Integer.parseInt(str,2);
String str1 = new Character((char)charCode).toString();

But it throws following: NumberFormatException: For input string : "[B@475e586c" 但是它引发以下内容: NumberFormatException: For input string : "[B@475e586c"

Sure the String is not an Integer but how can I retrieve the String value of a VARCHAR() FOR BIT DATA from DB2 in my java program? 确定字符串不是整数,但是如何在Java程序中从DB2检索VARCHAR() FOR BIT DATAString值?

As "[B" of toString indicates the type byte[] , the following should be possible: 由于toString “ [B”表示类型为byte[] ,因此应有以下可能:

byte[] b = (byte[]) resultset.getObject("OBJECT_ID");

Then there is the problem that String is for (Unicode) text, not binary data. 然后就是String是用于(Unicode)文本而不是二进制数据的问题。 A char is two bytes encoded in UTF-16. char是用UTF-16编码的两个字节。

You could try to abuse String as: 您可以尝试将String滥用为:

String s = new String(b, StandardCharsets.ISO_8859_1); // Bad idea.

This depends on OBJECT_ID. 这取决于OBJECT_ID。

Normally binary data could be encoded as text as Base64, using 64 ASCII chars as "digits." 通常,可以使用64个ASCII字符作为“数字”将二进制数据编码为Base64的文本。

String s = Base64.getEncoder().encodeToString(b);

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

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