/**
* Unicode编码规则:Unicode码对每一个字符用4位16进制数表示。
* 具体规则是:将一个字符(char)的高8位与低8位分别取出,转化为16进制数,
* 如果转化的16进制数的长度不足2位,则在高位补0,然后将高、低8位转成的16进制字符串拼接起来并在前面补上"\\u" 即可。
*
* @param str
* @return
*/
public static String convertStrToUnicode(String str) {
if (str == null || str.length() == 0) {
return "";
}
StringBuffer sb = new StringBuffer(1000);
// 临时变量
int hexNumber;
char c;
String hexStr;
for (int i = 0; i < str.length(); i++) {
// 加上\\u前缀
sb.append("\\u");
// 取出第i为的char字符
c = str.charAt(i);
// 取出高8位
hexNumber = (c >>> 8);
hexStr = Integer.toHexString(hexNumber);
if (hexStr.length() == 1)
sb.append("0");
sb.append(hexStr);
// 取出低8位
hexNumber = (c & 0xFF);
hexStr = Integer.toHexString(hexNumber);
if (hexStr.length() == 1)
sb.append("0");
sb.append(hexStr);
}
return sb.toString();
}
/**
* 将Unicode编码的字符串转换为String
*
* @param unicodeStr
* @return
*/
public static String convertUnicodeToStr(String unicodeStr) {
if (unicodeStr == null || unicodeStr.length() == 0) {
return "";
}
String unicodePrefix = "\\u";
String tempStr;
String tempHexStr;
StringBuffer sb = new StringBuffer(1000);
while (unicodeStr.contains(unicodePrefix)) {
// 获取第一次出现\\u的index
int firstIndex = unicodeStr.indexOf(unicodePrefix);
// 获取第二次出现\\u的index
int secondIndex = unicodeStr.indexOf(unicodePrefix, firstIndex + 2);
// 将第一出现与第二次出现中间的部分,截取下来
if (secondIndex == -1) {
tempStr = unicodeStr.substring(firstIndex);
} else {
tempStr = unicodeStr.substring(firstIndex, secondIndex);
}
tempHexStr = tempStr.substring(tempStr.indexOf(unicodePrefix) + 2);
if (tempHexStr.length() == 4) {
sb.append((char) Integer.parseInt(tempHexStr, 16));
}
// 将第二次出现以后的部分截取下来
if (secondIndex == -1) {
unicodeStr = "";
} else {
unicodeStr = unicodeStr.substring(secondIndex);
}
}
return sb.toString();
}
分享到:
相关推荐
utf-8、ANSI、Unicode相互转化c++实现 std::string ConverANSI2UTF8(const std::string & str); std::wstring ConverANSI2Unicode(const std::string str); std::wstring ConverUTF82Unicode(const std::string str)...
C++Builder String,UNICODE,UTF8,ANSI互转的类,类的互相转换
CString string_to_CString_unicode(std::string str) CString string_to_CString_unicode_Ex(std::string& str) CString string_to_CString_ansi(std::string str) CString string_to_CString_ansi_Ex(std::string&...
Unicode和中文的相互转换——VS2019的C++控制台项目(x64\x86\Debug\Release已编译) 1. bool ChineseToUnicode(CString cstr, string & str); 望断秋高和待雪初平__ChineseToUnicode:\u671b\u65ad\u79cb\u9ad8\u...
主要介绍了Java String字符串和Unicode字符相互转换代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了Java String字符串和Unicode字符相互转换代码,需要的朋友可以参考下
用于在PHP无法使用 iconv 函数和 mb_string 库的情况下,实现GB2312与UNICODE的相互转换。 正确转换的前提是你的系统是简体中文版的Windows。 包内附加一个UNICODE转换为GB2312的函数,看看里面的函数就明白了。 有...
多字节与UTF-8、Unicode之间的转换 ,里面有相互转换的6个函数 ,稍微修改下可以加到自己的c++程序中,比较好用
在VS2008中,默认的字符集形式是Unicode,但在VC6.0等工程中,默认的字符集形式是多...这里总结了在VS2008环境中 Unicode字符集下CString和char *之间相互转换的几种方法,其实也就是Unicode字符集与MBCS字符集转换。
本文实例讲述了JavaScript中字符串与Unicode编码互相转换的实现方法。分享给大家供大家参考,具体如下: 这段代码演示了JavaScript中字符串与Unicode编码的转换: // 为了控制台的演示方便, 变量没有添加 var 定义 /...
提供给android用户,GBK和汉字互相转换,Unicode和GBK互转。
s_str = s_unicode.encode(‘unicode-escape’).decode(‘string_escape’) 问题二: 将’\u810f\u4e71’转换为u’\u810f\u4e71′ 方法: s_str = ‘\u810f\u4e71’s_unicode = s_str.decode(‘unicode-escape’)...
问题: 将u’\u810f\u4e71’转换为’\u810f\u4e71′ 方法: s_unicode = u'\u810f\u4e71' ... 您可能感兴趣的文章:python文字和unicode/ascll相互转换函数及简单加密解密实现代码python2 中 uni
文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。Python 3不会以任意隐式的方式混用str和bytes,正是这使得两者的区分特别清晰。你不能拼接字符串和字节包,也无法在字节包里搜索字符串(反之亦然)...
big5,Unicode,GBK之间的相互转换,前提是只转换共同的字符集部分, 使用说明: 暂时程序支持以下字符编码方式: GBK,BIG5,UTF-16BE(Unicode big-endian字节顺序),UTF-16LE(Unicode little-endian字节顺序),UTF-8 ...
UTF-7 之间的互相转换,转换结果使用十六进制表示。 ******* STR ***************** * 运行环境 * 本软件在 Windows 98、Windows 2000、Windows XP 下测试通过,在其 它 Windows 操作系统下应该也能运行。 ...
本程序实现字符串与unicode码的相互转换
这将使线程能够互相独立地运行,而不会影响各自的错误代码。当函数返回给你时,它的返回值就能指明一个错误已经发生。若要确定这是个什么错误,请调用GetLastError函数: 见原书P4的程序(1) 该函数只返回线程的...
//unicode 编码,类似 \u4e2d\u6587 的相互转换, var str=chinese.string_to_unicode('中文'); console.log(str); var str=chinese.unicode_to_string('\u4e2d\u6587'); console.log(str); //unicode10 编码,类似 ...
我们看JSON语法中对于字符串的定义:是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。 下面我们来看一下在js中怎么把JSON格式的字符串...