Byte Array to String & back corruption

Discussion in 'Java' started by Ike, Nov 28, 2006.

  1. Ike

    Ike Guest

    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
    Ike, Nov 28, 2006
    #1
    1. Advertising

  2. Ike

    Danno Guest

    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
    Danno, Nov 28, 2006
    #2
    1. Advertising

  3. Ike

    Dale King Guest

    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
    Dale King, Nov 28, 2006
    #3
  4. Ike

    Dale King Guest

    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
    Dale King, Nov 29, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,208
    Roedy Green
    Aug 5, 2003
  2. Kirby
    Replies:
    3
    Views:
    625
    Kirby
    Oct 8, 2004
  3. Replies:
    20
    Views:
    9,698
    licebmi
    Sep 7, 2009
  4. Tom McGlynn
    Replies:
    4
    Views:
    841
    Mark Space
    Apr 19, 2008
  5. Replies:
    2
    Views:
    391
    Craig
    Feb 5, 2004
Loading...

Share This Page