4

I have a byte array with the value [B@6c89db9a I have stored this value in a MySQL db as a string representation - mybyteArray.toString()

I would like to retrieve the saved value from the database and create a new byte array using the string value of the original byte array.

I have tried the examples Java Byte Array to String to Byte Array

and byte to string and vice versa

However i am not getting the original byte array value. It is generating a different value. Can anyone please advise?

Community
  • 1
  • 1
Santiago
  • 982
  • 3
  • 14
  • 30
  • 2
    *I have a byte array with the value [B@6c89db9a I have stored this value ...* ... that's not a value. It's a completely useless (outside of Java) hashcode. The `toString()` method of an array does *not* print the contents of an array. – Brian Roach Jul 05 '13 at 23:59
  • yes, i am using it for authentication therefore i need to store it – Santiago Jul 06 '13 at 00:52

4 Answers4

11

I have a byte array with the value [B@6c89db9a I have stored this value ...

That's not a value. It's a completely useless (outside of Java) hashcode. The toString() method of an array does not print the contents of an array. Arrays do not override toString() and therefore Object.toString() is called.

If you wish to store an array of arbitrary bytes in MySQL you would want to use a BLOB type (or maybe VARBINARY? it's been a while since I've used MySQL but it appears from a quick google they're basically the same in modern versions) in your table, and store the bytes:

create table example (some_bytes BLOB);

Your prepared statement would look like:

String query = "INSERT INTO example (some_bytes) VALUES (?)";
PreparedStatement pstmt = conn.prepareStatement(query);

And you'd insert those bytes via:

pstmt.setBytes(1, myByteArray);
pstmt.execute();

Edit to add from comments: You have some bytes in an array. If those bytes represent a string of characters and you know what the character set is, you convert them to a String using the String constructor that takes a character set:

String myString = new String(bytes, knownCharset); 

For example, if they represent a UTF-8 string you would use:

String myString = new String(byteArray, Charset.forName("UTF-8"));
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • I am using a framework which doesnt support BLOB or CLOB. Therefore i thought that i could store the byte array using toString in the db and retrieve the string and recreate the original byte array. Is that possible? – Santiago Jul 06 '13 at 00:54
  • You thought ... wrong. The result of an array's `toString()` is completely and utterly meaningless outside of the JVM internals. – Brian Roach Jul 06 '13 at 00:55
  • OK so can you suggest how I can store the byte array in the dB without using blob etc? – Santiago Jul 06 '13 at 01:27
  • My answer covers the two possible solutions. The only other (ridiculous, hacky, no-one-would-ever-do-that) thing you could do is convert the byte array to an array of numbers in JSON and store it that way. – Brian Roach Jul 06 '13 at 16:44
4

For the record:

another solution would be to store the base64 encoded byte array, see https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html

The question is too old, so I'm not going to elaborate on this .

raudi
  • 1,701
  • 1
  • 16
  • 16
0

According to Object.toString()-

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.

In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Therefore what you are saving does not contain the real value of your byte array.

JHS
  • 7,761
  • 2
  • 29
  • 53
  • I cant use CLOB or BLOB due to a framework restriction. I am trying to save the binary array value in the DB as a string. Then retrieve the saved value and put it back as a byte array. Is that possible? – Santiago Jul 06 '13 at 00:55
0

You can try two functions below to finish your transform jobs:

    public static byte[] toBytes(String s) {
    try {
      return s.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
      System.out.println("UTF-8 not supported?", e);
      return null;
    }
    } 

      public static String toString(final byte [] b, int off, int len) {
          if (b == null) {
             return null;
          }
          if (len == 0) {
             return "";
          }
          try {
            return new String(b, off, len, "UTF8");
          } catch (UnsupportedEncodingException e) {
          System.out.println("UTF-8 not supported?", e);
          return null;
          }
      }
shuaizki
  • 11
  • 2