package unicode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CodeChange {
/*
* 把中文字符串转换为十六进制Unicode编码字符串
*/
public static String stringToUnicode(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
if (ch > 255)
str += "\\u" + Integer.toHexString(ch);
else
str += "\\" + Integer.toHexString(ch);
}
return str;
}
/*
* 把十六进制Unicode编码字符串转换为中文字符串
*/
public static String unicodeToString(String str) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(str);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
str = str.replace(matcher.group(1), ch + "");
}
return str;
}
public static void main(String[] args) {
// 直接以Unicode字符串的方式初始化字符串时,会自动
String s1 = "配置成功,重启后生效";
System.out.println("s1: " + s1);
//转换汉字为Unicode码
String s2 = "配置成功,重启后生效";
s2 = CodeChange.stringToUnicode(s2);
System.out.println("s2: " + s2);
//转换Unicode码为汉字
String s3 = CodeChange.unicodeToString(s2);
System.out.println("s3: " + s3);
}
}
分享到:
相关推荐
汉字与unicode转换工具
汉字与UNICODE转换,十进制和十六进制
汉字与UNICODE之间相互转换,使用起来相当方便。
汉字unicode转换器,方便汉字和UNICODE之间转换!
该工具支持汉字和unicode之间的相互转换器
汉字Unicode转换器,汉字Unicode可以相互转换
本源码是一个中文汉字与Unicode编码转换工具,采用WinForm进行开发,实现中文汉字与Unicode编码互相转换的功能。菜单功能:1、中文汉字转换成Unicode编码2、Unicode编码转换成中文汉字注意事项:1、开发环境为Visual...
汉字与unicode相互转换 上到下汉字转unicode下到上unicode转汉字
unicode 汉字转换器 unicode 汉字转换器 基于java编写,是个jar包,安装jdk后,直接双击
Java中文成Unicode码及从Unicode码转成中文
unicode汉字转换unicode汉字转换unicode汉字转换unicode汉字转换unicode汉字转换
汉字与UNICODE十进制十六进制转换 破解、汉化英文软件时很适用。
可以直接在c语言中使用,数组左列为unicode,根据unicode的数值大小从小到大进行排序,右列为对应的GB2312编码,旁边有注释对应的汉字,亲测可以在单片机上使用,建议用二分法搜索unicode然后转换为GB2312。...
汉字与UnIcode转换,UnIcode与汉字转换,二者互相转换工具。
Unicode和中文的相互转换——VS2019的C++控制台项目(x64\x86\Debug\Release已编译) 1. bool ChineseToUnicode(CString cstr, string & str); 望断秋高和待雪初平__ChineseToUnicode:\u671b\u65ad\u79cb\u9ad8\u...
excel 公式 自动转换unicode编码为汉字
实现Unicode和汉字的互相转码,解压后excel文档直接转换,方便快捷。
小巧好用的编码转换工具,输入中文,一点按钮就出来转换后的unicode