• Post Reply Bookmark Topic Watch Topic
  • New Topic

String encoding question  RSS feed

 
Xinyi Zhang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a byte[] bytes, from
new String(bytes); I got the correct string.
But from new String(bytes, "UTF-8"); I got the result "0". Why?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um - perhaps because the bytes represent "0" in UTF-8? Note that when you use new String(byte[]), the system uses a default encoding which depends on your system. The easiest way I know to find out what this default is is:

Yes, it seems like there should be a more straightforward way to find this out, but Java does not seem to provide one.
Anyway - to find out what's really going on, I think you need to know what values the bytes actually have:

Note the use of [ ] to clearly show the beginning and end. This is helpful if some of the characters are actually whitespace (which you can't see).
[ July 18, 2003: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!