• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ByteArray to String Conversion?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bill Ceglia
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What version of the JDK are you using?
 
Bill Ceglia
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, that worked perfect! Thank You Rob and Rikard, I greatly appreciate the help and your efforts!
 
Rikard Qvarforth
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic