Unicode conversion, Chinese character conversion, unicode encoding
Import java. util. regex. matcher; import java. util. regex. pattern; public class Translator {/*** convert Chinese characters to Unicode ** @ param s * @ return */public static String c2u (String s) {String ret = ""; string [] as = new String [s. length ()]; for (int I = 0; I <s. length (); I ++) {as [I] = Integer. toHexString (s. charAt (I) & 0 xffff); ret = ret + "\ u" + as [I];} return ret ;} /*** Unicode to Chinese Character *** @ param s * @ return */public static String u2c (String s) {String ret = ""; Pattern p = Pattern. compile ("(\ u (\ p {XDigit} {4})"); Matcher matcher = p. matcher (s); char ch; while (matcher. find () {ch = (char) Integer. parseInt (matcher. group (2), 16); ret = s. replace (matcher. group (1), ch + ""); s = ret;} return ret;} public void work (String mode, String content) {if (mode. equals ("U2C") {System. out. println (u2c (content);} else if (mode. equals ("C2U") {System. out. println (c2u (content);} else {System. err. println ("Error Mode, [MODE: U2c/C2U]") ;}}/*** @ param args */public static void main (String [] args) {if (args. length <2) {System. err. println ("Usage: \ n \ t [MODE (U2C/C2U)] <content>"); System. exit (1);} String mode = args [0]. trim (). toUpperCase (); String content = args [1]. trim (); new Translator (). work (mode, content );}}
This article is from the "new route" blog, please be sure to keep this source http://wporoad.blog.51cto.com/2924748/1301132