Byte Array to String & back corruption

I

Ike

I have an MS Word document, as a BLOB in a database.

byte [] jack = ..//an MS WORD Document, read into a byte array

//When I retrieve it from the DB, as a byte [], and write it to disk, the
file is fine.
//However, if I convert the byte array, to string, then back to byte array,
//it will now be unreadable from from Word if I write it to disk

String tempstring=new String(jack);
jack=tempstring.getBytes();

Can anyone tell me why? I thought these last two lines were entirely
reciprocal? Thanks, Ike
 
D

Danno

Ike said:
I have an MS Word document, as a BLOB in a database.

byte [] jack = ..//an MS WORD Document, read into a byte array

//When I retrieve it from the DB, as a byte [], and write it to disk, the
file is fine.
//However, if I convert the byte array, to string, then back to byte array,
//it will now be unreadable from from Word if I write it to disk

String tempstring=new String(jack);
jack=tempstring.getBytes();

Did you make sure that you are getting and setting the right encoding?
IOW,

byte[] myBytes = tempstring.getBytes("UTF-8");
String myString = new String(myBytes, "UTF-8");

Just a thought
 
D

Dale King

Ike said:
I have an MS Word document, as a BLOB in a database.

byte [] jack = ..//an MS WORD Document, read into a byte array

//When I retrieve it from the DB, as a byte [], and write it to disk, the
file is fine.
//However, if I convert the byte array, to string, then back to byte array,
//it will now be unreadable from from Word if I write it to disk

String tempstring=new String(jack);
jack=tempstring.getBytes();

Can anyone tell me why? I thought these last two lines were entirely
reciprocal? Thanks, Ike

Whether a conversion of arbitrary byte data to a String and back is
lossless depends on the character encoding. The default encoding on
Windoze is not lossless. Your example is using the default encoding.

While there is an encoding you could use that is lossless, the real
question is why do you want to convert it to a string since it is not
really a string? Why not convert it to a bitmap which is just as
nonsensical as converting it to a string?
 
D

Dale King

Danno said:
Ike said:
I have an MS Word document, as a BLOB in a database.

byte [] jack = ..//an MS WORD Document, read into a byte array

//When I retrieve it from the DB, as a byte [], and write it to disk, the
file is fine.
//However, if I convert the byte array, to string, then back to byte array,
//it will now be unreadable from from Word if I write it to disk

String tempstring=new String(jack);
jack=tempstring.getBytes();


Did you make sure that you are getting and setting the right encoding?

Note he said the bytes were the contents of a M$ Word document. There is
no right encoding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top