Meaningless Drivel is fun!*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "byte-string-byte convertion" Watch "byte-string-byte convertion" New topic
Author

byte-string-byte convertion

Alessandro Brawerman
Greenhorn

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);
while(true)
int bytesRead = rcfg_upt.read(block); // 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?
Thanks.
Pierre Henry
Ranch Hand

Joined: Oct 01, 2003
Posts: 31
Hi,
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: http://aspose.com/file-tools
 
subject: byte-string-byte convertion
 
Similar Threads
Streaming out data using ServletOutputStream
Trying to get a byte[] length using covered quotient
Reading image and convert it to Base64 format
File Read & Writing
Problem converting form data to UTF-8 on solaris