File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes String to bytes conversion. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String to bytes conversion." Watch "String to bytes conversion." New topic

String to bytes conversion.

sree kov

Joined: Jul 23, 2001
Posts: 15
If we construct a String using a byte array and then do get Bytes on the string object, are we guarenteed to get the same byte array contents ?
I am getting them differently.
Due to my project requirements we need a converter for any generic object to String and String to Obejct, so we planned to use Serialization for that
I am pasting the test code I am using and also the output. (SerialTestObject consists of one instance variable and getter method for that and implements Serializable interface)
public class SerialTest
public static void main(String args[]){
// create two objects
SerialTestObject object1 = new SerialTestObject();
// serialize the first one
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
byte[] temp1 = baos.toByteArray();

for(int g=0; g < temp1.length; g++)

String str = new String(temp1);

// read it back
byte[] temp2 = str.getBytes();
for(int g=0; g < temp2.length; g++)
ByteArrayInputStream bais = new ByteArrayInputStream(temp2);
ObjectInputStream ois = new ObjectInputStream(bais);
catch(Exception e){

Here is the output:
1160218410410511532105115329711032971161161141059811711610146 SerialTestObject; local class incompatible: stream classdesc serialVersionUID = 4610715130369482665, local class serialVersionUID = -8215536608381689943
at SerialTest.main(
B]If I use a static serialVersionUID the problem does not occur.[/B]
[added newlines for readability - Jim]
[ December 17, 2003: Message edited by: Jim Yingst ]
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Every Java class that implements "" has a serial version ID associated with it. The message you are seeing indicates that the data was saved with a different version of the SerialTestObject class that what you are using to read it in.
There is a way to fix this, but it may only work going forward (in other words you won't be able to read the old data, but if you write new data you'll be able to read it from now on). You need to run the following line command:

You need to set the classpath correctly, and provide the full path (w/ package name) to "SerialTestObject". The result of this command will look like:
SerialTestObjct: static final long serialVersionUID = 1000019706804773033L;
Copy everything starting with "static final ..." and paste it into your source file and re-compile. This will prevent the compiler from changing the ID every time you modify and re-compile the source.
Obviously if you add new data fields and try and read in old object instances it won't set those fields. But the serialized data should be compatible going forward.
I agree. Here's the link:
subject: String to bytes conversion.
It's not a secret anymore!