您的位置首页生活百科

利用JAVA将中文转Unicode编码

利用JAVA将中文转Unicode编码

的有关信息介绍如下:

利用JAVA将中文转Unicode编码

在程序开发过程中,经常需要将中文转为unicode编码,防止出现乱码等情况。在此给大家介绍两种将中文转为unicode的方法

安装JDK

找到JDK里面的native2ascii.exe文件,如图

输入汉字,点击回车,即可将汉字转为对应的unicode编码

或者打开MyEclipse,输入以下JAVA代码,则也可以将汉字转为对应的unicode编码,如图

public class Test{ public static String getStrUnicode(String inStr) { StringBuffer unicode = new StringBuffer(); char c; int bit; String tmp = null; for (int i = 0; i < inStr.length(); i++) { c = inStr.charAt(i); 晃暗 if (c > 255) { unicode.append("\\u"); bit = (c >悦侧>> 8); tmp = Integer.toHexString(bit); if (tmp.length() == 1) unicode.append("0"); unicode.append(tmp); 柱信级 bit = (c & 0xFF); tmp = Integer.toHexString(bit); if (tmp.length() == 1) unicode.append("0"); unicode.append(tmp); } else { unicode.append(c); } } return (new String(unicode)); } /** * 测试类 */ public static void main(String[] args) { String str = "Hello 中国"; System.out.println("转码前:" + str); System.out.println("转码后:" + getStrUnicode(str)); }}