/**
*判断是不是BMP code point
*BMP 是2的16次方,刚好两个字节,在两个字节以内的整数都可以转化为char
*/
public static boolean isBmpCodePoint(int codePoint) {
return codePoint >>> 16 == 0;//无符号向右移16位,如果codePoint不是16位的则不会等于0
}
/**
*判断是不是有效的code point,最大值是0x10ffff,小于等于这个值的都是有效的code point
*MAX_CODE_POINT=0X10FFFF
*/
public static boolean isValidCodePoint(int codePoint) {
int plane = codePoint >>> 16;//无符号向右移16位
return plane < ((MAX_CODE_POINT + 1) >>> 16);
}
/**
* java char字符都是用UTF-16编码的,而一个char是两个字节,最多表示2的16次方=65536种编码
* 而UTF-16的字符远远超过65536,为了解决这个问题,从char这65536个编号里,拿出2048个,规定它们
* 是surrogate
* 编号从U+D800至U+DBFF的规定为「High Surrogates」,共1024个。
* 编号为 U+DC00至U+DFFF的规定为「Low Surrogates」,也是1024个,
* High+Low组合在一起表示超过65536的UTF-16编码,又可以表示1048576种编码
*
* MIN_LOW_SURROGATE = '\uDC00'
*/
public static char lowSurrogate(int codePoint) {
return (char) ((codePoint & 0x3ff) + MIN_LOW_SURROGATE);
}
/**
* AC00-D7AF:朝鲜文音节 (Hangul Syllables)
* D800-DBFF:High-half zone of UTF-16
* DC00-DFFF:Low-half zone of UTF-16
* 可以看出,D7B0-D7FF是没有表示UTF-16的编码的,JAVA将这每表示的部分归到了High-Surrogate中
*
* MIN_HIGH_SURROGATE = '\uD800'
* MIN_SUPPLEMENTARY_CODE_POINT = 0x010000
*/
public static char highSurrogate(int codePoint) {
return (char) ((codePoint >>> 10)
//MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT >>> 10)=0xD7B0
+ (MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT >>> 10)));
}