将Unicode编码转换为汉字

来源:互联网 发布:转换视频格式软件 编辑:程序博客网 时间:2024/05/07 21:59

本代码针对Java语言,函数传入汉字的Unicode编码字符串,返回相应的汉字字符串,具体代码如下:【如果是json格式,下面这种Unicode码后面部分不会再转换回来,如Unicode码后面的  "}  部分不会转换出来】


[java] view plain copy
  1. public String convert(String utfString){  
  2.     StringBuilder sb = new StringBuilder();  
  3.     int i = -1;  
  4.     int pos = 0;  
  5.       
  6.     while((i=utfString.indexOf("\\u", pos)) != -1){  
  7.         sb.append(utfString.substring(pos, i));  
  8.         if(i+5 < utfString.length()){  
  9.             pos = i+6;  
  10.             sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));  
  11.         }  
  12.     }  
  13.       
  14.     return sb.toString();  
  15. }  


一、java,汉字转unicode码|unicode转汉字:   【如果是json格式下面这种Unicode码后面部分可以再转换出来,   转换出来是完整的

[java] view plain copy
  1. public class UnicodeStr {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         System.out.println(str2Unicode("最真不过平淡,最美不过平凡。"));  
  8.         System.out.println(unicode2Str("\u6700\u771f\u4e0d\u8fc7\u5e73\u6de1\uff0c\u6700\u7f8e\u4e0d\u8fc7\u5e73\u51e1\u3002"));  
  9.     }  
  10.   
  11.      public static String str2Unicode(String s)  
  12.       {  
  13.         String str = "";  
  14.         if ((s == null) || (s.trim().equals("")))  
  15.           return str;  
  16.         for (int i = 0; i < s.length(); i++)  
  17.         {  
  18.           byte[] bytes = String.valueOf(s.charAt(i)).getBytes();  
  19.           String s4;  
  20.           if (bytes.length == 1)  
  21.           {  
  22.             s4 = String.valueOf(s.charAt(i));  
  23.           } else {  
  24.             int ch = s.charAt(i);  
  25.             s4 = "\\u" + Integer.toHexString(ch);  
  26.           }  
  27.           str = str + s4;  
  28.         }  
  29.         return str;  
  30.       }  
  31.   
  32.      public static String unicode2Str(String theString) {  
  33.           char aChar;  
  34.           int len = theString.length();  
  35.           StringBuffer outBuffer = new StringBuffer(len);  
  36.           for (int x = 0; x < len;) {  
  37.            aChar = theString.charAt(x++);  
  38.            if (aChar == '\\') {  
  39.             aChar = theString.charAt(x++);  
  40.             if (aChar == 'u') {  
  41.              int value = 0;  
  42.              for (int i = 0; i < 4; i++) {  
  43.               aChar = theString.charAt(x++);  
  44.               switch (aChar) {  
  45.               case '0':  
  46.               case '1':  
  47.               case '2':  
  48.               case '3':  
  49.               case '4':  
  50.               case '5':  
  51.               case '6':  
  52.               case '7':  
  53.               case '8':  
  54.               case '9':  
  55.                value = (value << 4) + aChar - '0';  
  56.                break;  
  57.               case 'a':  
  58.               case 'b':  
  59.               case 'c':  
  60.               case 'd':  
  61.               case 'e':  
  62.               case 'f':  
  63.                value = (value << 4) + 10 + aChar - 'a';  
  64.                break;  
  65.               case 'A':  
  66.               case 'B':  
  67.               case 'C':  
  68.               case 'D':  
  69.               case 'E':  
  70.               case 'F':  
  71.                value = (value << 4) + 10 + aChar - 'A';  
  72.                break;  
  73.               default:  
  74.                throw new IllegalArgumentException(  
  75.                  "Malformed      encoding.");  
  76.               }  
  77.   
  78.              }  
  79.              outBuffer.append((char) value);  
  80.             } else {  
  81.              if (aChar == 't') {  
  82.               aChar = '\t';  
  83.              } else if (aChar == 'r') {  
  84.               aChar = '\r';  
  85.              } else if (aChar == 'n') {  
  86.               aChar = '\n';  
  87.              } else if (aChar == 'f') {  
  88.               aChar = '\f';  
  89.              }  
  90.              outBuffer.append(aChar);  
  91.             }  
  92.            } else {  
  93.             outBuffer.append(aChar);  
  94.            }  
  95.   
  96.           }  
  97.           return outBuffer.toString();  
  98.   
  99.          }  
  100.   
  101. }  

二、还有一种文字转unicode方法就是安装了java的jdk后就可以用bin目录下的native2ascii.exe来转换。先将要转换的文字写入一个文本文件,如str.txt ,则在命令行方式下键入命令:native2ascii str.txt (这个网上看到的,未测试)








原创粉丝点击