unicode编码
来源:https://blog.csdn.net/qq_22771739/article/details/84261165
Unicode 是全球文字统一编码。它把世界上的各种文字的每一个字符指定唯一编码,实现跨语种、跨平台的应用。
Unicode 只是一个符号集,它只规定了每个符号的二进制数,却没有规定这个二进制数应该如何存储。比如,汉字‘严’的 Unicode 是十六进制数4E25,转换成二进制数足足有15位(100111000100101),也就是说,这个符号的表示至少需要2个字节。表示其他更大的符号,可能需要3个字节或者4个字节,甚至更多。
Java的class文件采用utf8的编码方式,Java的字符串是unicode编码的
将字符串与 unicode 相互转换的工具类
内容来源 https://www.cnblogs.com/poterliu/p/9579918.html
unicode 编码规则 :
unicode 码对每一个字符用4位16进制数表示。具体规则是:将一个字符(char)的高8位与低8位分别取出,转化为16进制数,如果转化的16进制数的长度不足2位,则在其后补0,然后将高、低8位转成的16进制字符串拼接起来并在前面补上"\u" 即可。
package sherry.com.javalib.base;
/**
* 字符串与unicode的相互转换工具类
*/
public class UnicodeConvertUtil {
private static boolean isBig = true;
/**
* 将String内容转成Unicode,且忽略\\u
*
* @param str
* @return
*/
public static byte[] putString2UnicodeBytes(String str) {
return putString2UnicodeBytes(str, isBig);
}
/**
* 将字符串转成unicode
*
* @param str 待转字符串
* @return unicode字符串
*/
public static byte[] putString2UnicodeBytes(String str, boolean isBig) {
str = (str == null ? "" : str);
char c;
int i, j;
byte[] strByteRes = new byte[str.length() * 2];
for (i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (isBig) { // 大端
j = (c >>> 8); // 取出高8位
strByteRes[2 * i] = (byte) j;
j = (c & 0xFF); // 取出低8位
strByteRes[2 * i + 1] = (byte) j;
} else { // 小端
j = (c & 0xFF); // 取出低8位
strByteRes[2 * i] = (byte) j;
j = (c >>> 8); // 取出高8位
strByteRes[2 * i + 1] = (byte) j;
}
}
return strByteRes;
}
/**
* 将字符串转成unicode
*
* @param str
* @return
*/
public static String putString2UnicodeString(String str) {
str = (str == null ? "" : str);
String tmp;
StringBuffer sb = new StringBuffer(1000);
char c;
int i, j;
sb.setLength(0);
for (i = 0; i < str.length(); i++) {
c = str.charAt(i);
sb.append("\\u");
j = (c >>> 8); //取出高8位
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
j = (c & 0xFF); //取出低8位
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
}
return (new String(sb));
}
/**
* 将16进制表示的unicode转成中文,开头不含\\u
*
* @param unicodeBytes
* @return
*/
public static String unicodeBytes2Str(byte[] unicodeBytes) {
return unicodeBytes2Str(unicodeBytes, true);
}
/**
* \\u5f20 new byte[]{0x5f,0x20}
*
* @param unicodeBytes
* @param isBig
* @return
*/
public static String unicodeBytes2Str(byte[] unicodeBytes, boolean isBig) {
String strRes = "";
if (null == unicodeBytes) {
return strRes;
}
byte[] valueBytes = new byte[2];
for (int i = 0; i < unicodeBytes.length; i += 2) {
if (isBig) {
valueBytes[0] = unicodeBytes[i];
valueBytes[1] = unicodeBytes[i + 1];
// strRes += ((char) Integer.valueOf(TBaseNumber.byte2HexString(valueBytes), 16).intValue());
strRes += TBaseNumber.byte2Char(valueBytes);
} else {
valueBytes[0] = unicodeBytes[i + 1];
valueBytes[1] = unicodeBytes[i];
// strRes += ((char) Integer.valueOf(TBaseNumber.byte2HexString(valueBytes), 16).intValue());
strRes += TBaseNumber.byte2Char(valueBytes);
}
}
return strRes;
}
public static String unicodeToCn(String unicode) {
/** 以 \ u 分割,因为java注释也能识别unicode,因此中间加了一个空格 */
String[] strs = unicode.split("\\\\u");
String returnStr = "";
// 由于unicode字符串以 \ u 开头,因此分割出的第一个字符是""。
for (int i = 1; i < strs.length; i++) {
returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
}
return returnStr;
}
}
测试:
public static void main(String[] args) {
String characketStr = "测试";
System.out.println("-----字符转成 unicode 编码(带\\u):" + UnicodeConvertUtil.putString2UnicodeString(characketStr));
System.out.println("-----字符转成 unicode[] :" + TBaseNumber.byte2HexString(UnicodeConvertUtil.putString2UnicodeBytes(characketStr, true)));
byte[] unicodeByte = TBaseNumber.hexStringToByte("6D4B8BD5");
System.out.println("-----unicode[] 转 字符:" + UnicodeConvertUtil.unicodeBytes2Str(unicodeByte, true));
System.out.println("-----unicode编码 转 字符:" + UnicodeConvertUtil.unicodeToCn("\\u6d4b\\u8bd5"));
}
结果:
-----字符转成 unicode 编码(带\u):\u6d4b\u8bd5
-----字符转成 unicode[] :6D4B8BD5
-----unicode[] 转 字符:测试
-----unicode编码 转 字符:测试
TBaseNumber工具类
https://www.jianshu.com/p/55e1545f2d45
参考:
字符编码的奥秘utf-8, Unicode
https://blog.csdn.net/hherima/article/details/8655200