将Unicode编码转换为汉字

本代码针对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 (这个网上看到的,未测试)








  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Python内置的decode()函数将Unicode编码转换汉字。例如,如果要将Unicode编码为"\u4e2d\u6587"转换汉字,可以使用以下代码: unicode_str = "\u4e2d\u6587" chinese_str = unicode_str.encode('utf-8').decode('unicode_escape') print(chinese_str) 输出结果为:"中文"。 ### 回答2: 在Python中,我们可以使用`chr()`函数将Unicode编码转换汉字Unicode是一种标准化的字符集,它为世界上几乎所有的字符定义了独一无二的数值编码。而汉字Unicode字符集中的一部分。 要将Unicode编码转换汉字,可以使用以下代码: ```python unicode_code = 27721 # 要转换Unicode编码 hanzi = chr(unicode_code) # 使用chr()函数将Unicode编码转换汉字 print(hanzi) # 输出转换后的汉字 ``` 在上面的代码中,`unicode_code`是要转换Unicode编码,可以根据需要更改此值。`chr()`函数用于将Unicode编码转换为对应的字符。 当我们运行上述代码时,将输出转换后的汉字。 需要注意的是,转换的前提是指定的Unicode编码对应于汉字字符。在Unicode中,汉字字符的编码范围是0x4E00到0x9FFF,您可以根据需要更改`unicode_code`变量的值来指定不同的汉字编码。 另外,还可以使用`ord()`函数将汉字转换Unicode编码。使用方法与`chr()`函数相反。具体代码如下: ```python hanzi = '你' # 要转换汉字 unicode_code = ord(hanzi) # 使用ord()函数将汉字转换Unicode编码 print(unicode_code) # 输出转换后的Unicode编码 ``` 以上就是使用Python将Unicode编码转换汉字的方法。 ### 回答3: 在Python中将Unicode编码转换汉字可以使用`chr()`函数。`chr()`函数将Unicode编码作为参数,返回对应的字符。使用以下步骤将Unicode编码转换汉字。 首先,确定Unicode编码的值。例如,要将Unicode编码`U+6C49`转换汉字。 ```python unicode_code = 0x6C49 ``` 然后,使用`chr()`函数将Unicode编码转换为对应的字符。 ```python chinese_character = chr(unicode_code) ``` 最后,打印输出转换后的汉字。 ```python print(chinese_character) ``` 运行代码后,将输出转换后的汉字。 完整的代码示例: ```python unicode_code = 0x6C49 chinese_character = chr(unicode_code) print(chinese_character) ``` 以上代码将输出`汉`,即Unicode编码`U+6C49`对应的汉字。 需要注意的是,Python默认使用的是UTF-8编码,因此在处理Unicode编码时,确保当前环境的编码设置正确。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值