Reputation: 982
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?
Upvotes: 4
Views: 35808
Reputation: 1730
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 .
Upvotes: 4
Reputation: 76918
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"));
Upvotes: 11
Reputation: 11
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;
}
}
Upvotes: 0
Reputation: 7871
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.
Upvotes: 0