String and Bit Array conversion issues
Here is my code:
byte[] temp1 = EMan.EncodeDataString(ExportStudent.FirstName);
System.err.println("Original String");
System.err.println(ExportStudent.FirstName);
System.err.println("Original byte array");
System.err.println(temp1);
String STemp10 = temp1.toString();
System.err.println("Transferred String");
System.err.println(STemp10);
byte[] temp23 = STemp10.getBytes();
System.err.println("Transferred byte array");
System.err.println(temp23);
Here is my log:
Original String
Nelson
Original byte array
[B@2ac79769
Transferred String
[B@2ac79769
Transferred byte array
[B@15a58470
Why is the conversion back to a Byte array screwing it up?
Re: String and Bit Array conversion issues
B@15a58470 etc is the result of printing an objects default toString() method.
Note that calling toString() doesn't necessarily mean it prints out the byte array for you.
For more information about toString(), ill refer you to this neat little article.
Java toString Method | Java Beginner
Re: String and Bit Array conversion issues
From the toString method of Object in the Java API:
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())
Since arrays are object and do not have their own toString method they inherit the toString from Object. The above explains the output you are seeing.
Re: String and Bit Array conversion issues
Try using the Arrays toString() method for printing out the contents of arrays.
The [B@15a58470 String is what is generated by the array object's to String method. It's saying there is an array ([) of bytes(B) at memory location: 15a58470