It's not a secret anymore!*
The moose likes Beginning Java and the fly likes ByteArray to String Conversion? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ByteArray to String Conversion?" Watch "ByteArray to String Conversion?" New topic
Author

ByteArray to String Conversion?

Bill Ceglia
Ranch Hand

Joined: Dec 13, 2001
Posts: 30
I am testing converting a string to a bytearray and then back to a string. The first part works fine but when I try and convert from the bytearray to the string the data appears not to be correct? Below is the code:
String test = "TEST";
byte bytearrayin[] = new byte[128];
bytearrayin = test.getBytes();

/*This is the line that returns "incorrect data"*/
String teststr = bytearrayin.toString();
Rikard Qvarforth
Ranch Hand

Joined: Jul 10, 2001
Posts: 107
Hi, maby this will help.
String test = "TEST";
byte bytearrayin[] = new byte[3];
bytearrayin = test.getBytes();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bs.write(bytearrayin,0,4);
System.out.println(bs.toString());
Hope it helps
Rille
Bill Ceglia
Ranch Hand

Joined: Dec 13, 2001
Posts: 30
I am trying to assign the bytes to a string and when I do that I see this data in the bs.Value field "java.io.ByteArrayOutputStream@8910998"
Here is the code:
bytearrayin = test.getBytes();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bs.write(bytearrayin,0,test.length());
/*The assignment using toString doesn't seem to work*/
gpsinstr = bs.toString();
Rikard Qvarforth
Ranch Hand

Joined: Jul 10, 2001
Posts: 107
hi humm the code below works fine .
class test {
public static void main(String args[]){
String test = "TEST";
byte bytearrayin[] = new byte[3];
bytearrayin = test.getBytes();
ByteArrayOutputStream bs = new ByteArrayOutputStream ();
bs.write(bytearrayin,0,test.length());
System.out.println(bs.toString());
String teststr = bs.toString();
System.out.println(teststr);
}
}
but the output you have looks like you are printing out the heap adress of the ByteArrayOutputStream object try the code above and see if you got the output you want..
rille
Bill Ceglia
Ranch Hand

Joined: Dec 13, 2001
Posts: 30
Rikard,
I tried the sample code you gave me and again it placed the string "java.io.ByteArrayOutputStream@8922308" in the teststr Strings value field, notice the number is different. I am debugging using CodeWarrior for Java and I step through each line and it all seems to work fine and the assignment line executes fine but when I view the "Value" field for the string it has the above mentioned data in it?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
You both have a different understand about what the "correct" behavior is. Rikard is correct that the toString() method is working correctly, but Bill is confused because he is expecting different behavior.
First, the toString() method is inherited from Object, so every object always has this method. It is intended to provide some "meaningful representation" of the object's state, but this is highly context-dependant.
The default behaviour in Object is to just print out the name of the object's class, and a reference to that object's pointer/unique identifier in memory.

bytearrayin is a byte[], and inherits Object's toString() method. When you call toString() on an array, you get the name of the object's class, and address/unique identifier for that instance.
This is the correct behavior. If you don't like the default implementation of toString() , you can always write your own (except for an array of course, since you can't create subclasses of array types.)

Bill, the example that Rikard posted last works fine on my machine, so make sure you've copied it exactly and try it again, it should work.

Rob


Rob
SCJP 1.4
Bill Ceglia
Ranch Hand

Joined: Dec 13, 2001
Posts: 30
I have tried, by cut and paste method, and I still receive the same results?! The System.out.println statement prints exactly what I stated previously, I never see the bytes converted from their decimal value to an ASCII character??? When the System.out.println prints it displays in a dos window, used in the CodeWarrior debugger, and shows "java.io.ByteArrayOutputStream@8810684" for both print statements. I don't know what else to tell you except what I see.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
What version of the JDK are you using?
Bill Ceglia
Ranch Hand

Joined: Dec 13, 2001
Posts: 30
I am using JDK1.2.2, the CodeWarrior debugger only works with this version and nothing newer, so that's why I'm not using JDK1.3.1.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Ok, I just checked the bugparade at Sun and that's indeed a bug in the version of the JDK you are using...toString() has not been implmented.
Here's a work around:

replace
String teststr = bs.toString();
with
String teststr = new String(bs.toByteArray());

That should work (at least for 8 bit encoding schemes; this will break under a 16 bit encoding)
Rob
Bill Ceglia
Ranch Hand

Joined: Dec 13, 2001
Posts: 30
Ok, that worked perfect! Thank You Rob and Rikard, I greatly appreciate the help and your efforts!
Rikard Qvarforth
Ranch Hand

Joined: Jul 10, 2001
Posts: 107
thx Rob for your great work i dident know a bout the bug
(excuse me for my bad english)
rille
 
Don't get me started about those stupid light bulbs.
 
subject: ByteArray to String Conversion?
 
Similar Threads
read and write file in parts
EasyMock for InputStream.read(byte[] b)
Reading image and convert it to Base64 format
[URLyBird] Should I use a business object or String[] in my Data class?
URLBirdy(1.3.2) Create method