• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string to byte array

 
Dexter O'Connell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
im reading the contents of a gif into a string and i need to out put this into another gif file. ( at the moment im not manipulating any of the images data )

ive read the contents of the image into a byte array.
the byte array is then converted into a string.

im trying to convert this string back into a byte array and then output this new file.

the code seem to be going in correct but when i go to open the file it says "error opening file"

can any of ye see what im doing wrong?

heres the code.

cheers for the help

dex
 
Dun Dagda
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

perhaps there is something wrong with your definition of the reference variable to the File object fileTestOutput. Maybe it is pointing to a file that is read-only or to a directory that you don't have write access to?

I suggest that you post more of the exact error message that is being returned. Are you using a Java program to read the file that you have written? It is not clear from your post exactly what you are doing.

You might also want to take a look at the getBytes() method of the String class, which might help you do what you want more efficiently.

DD
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dexter O'Connell:

can any of ye see what im doing wrong?


Hi Dexter,

Yeah, well, how would I put it? The idea of putting binary
data in a String is wrong. Remember that java Strings are not
like char* in c. Java Strings are encoded in unicode, which means
that after you make your conversion from bytearray to string,
the byte on 86th position value of which was 231 is not a byte
anymore and it's value does not have to be 231 anymore. And when
you convert back to byte array, char value (which is in range
0-65535) is converted (casted in your case) to a byte value (0-255).

I wouldn't be surprised if something got lost... Do those 2 files have
the same length? How about content?

Best regards,
Petr
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic