• Post Reply Bookmark Topic Watch Topic
  • New Topic

String to bytes conversion.  RSS feed

Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
java.io.InvalidClassException: SerialTestObject; local class incompatible: stream classdesc serialVersionUID = 4610715130369482665, local class serialVersionUID = -8215536608381689943
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:454)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1511)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1425)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1616)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1264)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:322)
at SerialTest.main(SerialTest.java:34)
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 ]
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every Java class that implements "java.io.Serializable" 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!