my dog learned polymorphism
The moose likes Beginning Java and the fly likes string to byte array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string to byte array" Watch "string to byte array" New topic

string to byte array

Dexter O'Connell

Joined: Oct 05, 2004
Posts: 9
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


If you build it..... HE will come
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54

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.


SCJP 1.4<br />SCWCD (in progress)
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
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,

Get a better web browser:<br /><a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: string to byte array
It's not a secret anymore!