Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UTF-8 & UTF-16 Format

 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a problem to print string value in utf-8 format. The Given below function produce following outputs.

String out="Raghu";
System.out.println (new String(out.getBytes("utf-16"),"UTF-8")); gives ??
System.out.println (new String(out.getBytes("utf-16"),"UTF-16")); gives aRhg
System.out.println (new String(out.getBytes("utf-8"),"UTF-16")); gives ??

My problem is how to convert string values in utf-8 to utf-16 and utf-16 to utf-8. Could any one enlighten me?
 
henry akoma
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from what i know in basic encoding/decoding of data(though i suggest u verify this),the encoding and decoding ends should share the same protocol; that means both should use the same scheme.

thus, when u encode with "UTF-8", i think it is advisable to decode with "UTF-8".same goes for "utf-16".

try this:
....
System.out.println(new String(out.getBytes("utf-8"),"utf-8");
OR
System.out.println(new String(out.getBytes("utf-16"),"utf-16");
[ June 24, 2005: Message edited by: henry akoma ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition, if you're looking at the output in a terminal window, the terminal may not understand Unicode, and thus be unable to display it properly. In that case, write the output to a file and look at the result in a Uniode-capable editor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic