• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retrieve charset information from a String object

 
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

This question already tackles the following problem, but the answer seems a bit blurry to me.
If you can create a String object with the following constructor ( link: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#String%28byte[],%20java.nio.charset.Charset%29 ), then I guess this charset information is stored in the String object somewhere. Is there a way to retrieve this information?

From what I understand, encoding and underlaying bytes of data are not connected, only with correct encoding information you can display text correctly (from raw bytes).
However, String can be constructed with encoding in mind (and consequently it is not just plain byte array), probably there is a way to retrieve it somehow?
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The charset is not stored in the String. The string content encoding is implicit and is always UNICODE stored as UTF16 code points. The charset parameter is required for the constructor to be able to know how to convert the bytes to UTF16 code points.
 
Rade Koncar
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, you're right. All strings are internally stored in common format, so you can for example compare them regardless of their encoding (I checked this).
 
reply
    Bookmark Topic Watch Topic
  • New Topic