Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

byte-string-byte convertion

 
Alessandro Brawerman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic