aspose file tools*
The moose likes Java in General and the fly likes UTF-8 & UTF-16 Format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "UTF-8 & UTF-16 Format" Watch "UTF-8 & UTF-16 Format" New topic
Author

UTF-8 & UTF-16 Format

Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
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?


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
henry akoma
Greenhorn

Joined: May 17, 2005
Posts: 21
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
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
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.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: UTF-8 & UTF-16 Format