Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Byte Array to String & back corruption

Reply
Thread Tools

Byte Array to String & back corruption

 
 
Ike
Guest
Posts: n/a
 
      11-28-2006
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


 
Reply With Quote
 
 
 
 
Danno
Guest
Posts: n/a
 
      11-28-2006

Ike wrote:
> 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

 
Reply With Quote
 
 
 
 
Dale King
Guest
Posts: n/a
 
      11-28-2006
Ike wrote:
> 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?

--
Dale King
 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      11-29-2006
Danno wrote:
> Ike wrote:
>
>>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.

--
Dale King
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert form byte[4] to Int32 while retaining the binary value of the byte array jeff@foundrymusic.com C++ 20 09-07-2009 08:54 PM
Re: how to extend a byte[] array with a null byte? Tom McGlynn Java 4 04-18-2008 11:49 PM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM
Byte array to string and back - newbie question popsovy@rusmex.com ASP .Net Security 2 02-05-2004 07:47 PM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments