Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
1  
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 5 '13 at 23:59
    
yes, i am using it for authentication therefore i need to store it – Santiago Jul 6 '13 at 0:52
up vote 0 down vote accepted

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"));
share|improve this answer
    
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 6 '13 at 0:54
    
You thought ... wrong. The result of an array's toString() is completely and utterly meaningless outside of the JVM internals. – Brian Roach Jul 6 '13 at 0:55
    
OK so can you suggest how I can store the byte array in the dB without using blob etc? – Santiago Jul 6 '13 at 1:27
    
any ideas of what can be done?? – Santiago Jul 6 '13 at 16:43
    
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 6 '13 at 16:44

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.

share|improve this answer
    
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 6 '13 at 0:55

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 .

share|improve this answer

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;
          }
      }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.