Java中Unicode编码和汉字之间的转换
来源:互联网 发布:ubuntu网络连接不上 编辑:程序博客网 时间:2024/05/16 08:32
// 将中文转换为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)
// toHexString() 方法返回为无符号整数基数为16的整数参数的字符串表示形式
str += "\\u" + Integer.toHexString(ch);
else
str += "\\" + Integer.toHexString(ch);
}
return str;
}
// 完成Unicode转换为字符串
public static String unicodeToString(String str) {
// 关于"(\\\\u(\\p{XDigit}{4}))$/"中\\\\第一个和第三个表示转义;
// "(\\\\u(\\p{XDigit}{4}))$/"也可以写成"(/u(\\p{XDigit}{4}))$/"
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 String stringToUnicode(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
if (ch > 255)
// toHexString() 方法返回为无符号整数基数为16的整数参数的字符串表示形式
str += "\\u" + Integer.toHexString(ch);
else
str += "\\" + Integer.toHexString(ch);
}
return str;
}
// 完成Unicode转换为字符串
public static String unicodeToString(String str) {
// 关于"(\\\\u(\\p{XDigit}{4}))$/"中\\\\第一个和第三个表示转义;
// "(\\\\u(\\p{XDigit}{4}))$/"也可以写成"(/u(\\p{XDigit}{4}))$/"
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;
}
0 0
- Java中Unicode编码和汉字之间的转换
- 关于JAVA中汉字和Unicode的值之间的转换
- unicode和中文汉字之间的转换
- JAVA中GB2312与Unicode之间的编码转换
- 关于中日文和UNICODE之间编码的转换
- 中日文和UNICODE之间编码的转换
- Android- Unicode编码 和 汉字转换( java实现 , javascript实现)
- 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)
- java中unicode utf-8以及汉字之间的转换工具类
- java中unicode utf-8以及汉字之间的转换工具类
- 汉字和Unicode码之间的转换(Pack/Unpack)
- java将Unicode编码转换为汉字
- java Unicode编码转换为汉字
- java将Unicode编码转换为汉字
- Java:转换汉字为unicode形式的字符串和转换unicode形式字符串转换成汉字
- unicode与汉字之间的转换
- Python中unicode编码的字符串和其他格式的字符串之间进行转换
- QT中Unicode编码与字符串之间的转换
- 黑马程序员--C语言关键字,注释
- 打开CCS5 出现locking is not possible in the directory
- 黑马程序员--递归方法参数的自增自减
- Java集合3:LinkedHashMap的实现原理
- CSU1659: Graph Center(最短路)
- Java中Unicode编码和汉字之间的转换
- HelloVelocity
- 用PowerShell把东西pin到开始菜单上
- Java集合4:LinkedHashSet的实现原理
- 路遥《人生》读后感
- C++文件读写详解(ofstream,ifstream,fstream)
- Java集合5:LinkedHashMap与LRU cache
- Java集合6:ArrayList的实现原理
- js正则匹配数字、大小写字母、下划线、中线和点