Unicode and Chinese translation in Java

Source: Internet
Author: User

public class Test
{
public static void Main (string[] args)
{
String s = "Set export template in transit";
String TT = gbencoding (s);
}

public static string gbencoding (final string gbstring)
{
char[] utfbytes = Gbstring.tochararray ();
String unicodebytes = "";
for (int byteindex = 0; Byteindex < utfbytes.length; byteindex++)
{
String Hexb = integer.tohexstring (Utfbytes[byteindex]);
if (Hexb.length () <= 2)
{
Hexb = "XX" + Hexb;
}
Unicodebytes = unicodebytes + "\\u" + Hexb;
}
System.out.println ("Unicodebytes is:" + unicodebytes);
return unicodebytes;
}

public static string Decodeunicode (final string datastr)
{
int start = 0;
int end = 0;
Final StringBuffer buffer = new StringBuffer ();
while (Start >-1)
{
End = Datastr.indexof ("\\u", start + 2);
String charstr = "";
if (end = =-1)
{
Charstr = datastr.substring (start + 2, datastr.length ());
}
Else
{
Charstr = datastr.substring (start + 2, end);
}
Char letter = (char) integer.parseint (CHARSTR, 16); 16 binary parse-shaped string.
Buffer.append (new Character (letter). ToString ());
start = end;
}
return buffer.tostring ();
}
}

Unicode and Chinese translation in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.