File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes byte-string-byte convertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "byte-string-byte convertion" Watch "byte-string-byte convertion" New topic

byte-string-byte convertion

Alessandro Brawerman

Joined: Sep 22, 2003
Posts: 22
Hi all,
I have a byte array and I convert it to string during the execution of my program. After that, I have to convert the string to byte array again but when I compare the original byte array with the one that was converted by the string they are different. I'm using the "UTF8" encode to do all that. Here's a more precise explanation:
I have this binary file and when I see its contents in linux I get the following result:
???IL ??I??4:?w?
In my program I read this to a byte array and convert it to string as follows:
byte[] block = new byte[16];
FileInputStream rcfg_upt = new FileInputStream(upt);
int bytesRead =; // Copies file contents to block
if (bytesRead == -1) break;
String str = new String(block, "UTF8");
After that I copy the string str to a byte array:
byte[] back = str.getBytes("UTF8");
Problem is when I print the new byte array or when I save it to an output file I get the following value:
IL I 4: w
a different value from the original.
Is there a way to get this right?
Pierre Henry
Ranch Hand

Joined: Oct 01, 2003
Posts: 31
It seems like you probably read an int in place of a real char
and get these garbage output.
Take a look at essentials

"A good method shouldn't bypass one screen long"<br />"genius is the knowledge left when one has forgotten everything"
I agree. Here's the link:
subject: byte-string-byte convertion
jQuery in Action, 3rd edition