Package com. util;
Import java. Io. unsupportedencodingexception;
Public class unicodeutil
{
/**
*
* @ Param gbstring
* @ Return
*/
Public static string encodeunicode (final string data)
{
Char [] utfbytes = data. tochararray ();
String unicodebytes = "";
For (char utfbyte: utfbytes)
{
String hexbyte = integer. tohexstring (utfbyte );
If (hexbyte. Length () <= 2)
{
Hexbyte = "00" + hexbyte;
}
Unicodebytes + = "// U" + hexbyte;
}
Return unicodebytes;
}
Public static string decodeunicode (final string data)
{
Int start = 0;
Int end = 0;
Final stringbuilder buffer = new stringbuilder ();
While (Start>-1)
{
End = data. indexof ("// U", start + 1 );
String charstr = "";
If (END =-1)
{
Charstr = data. substring (data. Length ()-4, Data. Length ());
}
Else
{
Charstr = data. substring (end-4, end );
}
Char letter = (char) integer. parseint (charstr, 16 );
Buffer. append (new character (letter). tostring ());
Start = end;
}
Return buffer. tostring ();
}
Public static void main (string [] ARGs) throws unsupportedencodingexception
{
String STR = "sorry, this file type is not supported currently! ";
String Unicode = encodeunicode (STR );
System. Out. println (UNICODE );
String decode = decodeunicode (UNICODE );
System. Out. println (decode );
}
}