• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

String encoding question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic