0

I have a variable OBJECT_ID stored in DB2 as OBJECT_ID VARCHAR() FOR BIT DATA NOT NULL.

How can I retrieve the String value stored in it?

I have tried following code to convert Binary to String in Java as:

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"

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?

6
  • 2
    Please don't use too much capital and bold. Commented May 15, 2018 at 7:14
  • Looks like that this needs db2. Commented May 15, 2018 at 7:16
  • try this String str = resultset.getString("OBJECT_ID"); Commented May 15, 2018 at 7:18
  • @YCF_L Now it returns a String as "0464145d43B3fd4c932bf480df3b4a32" and same exception Commented May 15, 2018 at 7:25
  • you said Sure the String is not an Integer so the question is, why you parse it as an int? Commented May 15, 2018 at 7:29

1 Answer 1

2

As "[B" of toString indicates the type byte[], the following should be possible:

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

Then there is the problem that String is for (Unicode) text, not binary data. A char is two bytes encoded in UTF-16.

You could try to abuse String as:

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

This depends on OBJECT_ID.

Normally binary data could be encoded as text as Base64, using 64 ASCII chars as "digits."

String s = Base64.getEncoder().encodeToString(b);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.