2017八月11
UTF-8中文字符编码和解码,用位运算实现
/** * UTF中文字符编码和解码 * 中文字符占3个字节,前缀分别是:1110XXXX 10XXXXXX 10XXXXXX * * @author FreeDroid * */ public class Utf8codeANDdecode { public static void main(String[] args) { int ch = '我'; byte[] bytes = codeUTF8(ch); char ch2 = decodeUTF8(bytes); System.out.println(ch2); } /** * 解码 * @param bytes * @return */ public static char decodeUTF8(byte[] bytes) { int ch = (bytes[0]<<12&0xffff)|(bytes[1]<<6&0x3fff)|(bytes[2]&0x3f); return (char) ch; } /** * 编码 * @param ch * @return */ public static byte[] codeUTF8(int ch) { int b3 = ch & 0x3f | 0x80; int b2 = ch >>> 6 & 0x3f | 0x80; int b1 = ch >>> 12 & 0xf | 0xe0; return new byte[] { (byte) b1, (byte) b2, (byte) b3 }; } }
最新评论
不错,有用!!!
说领导 想来你这借碗 好茶水 喝喝...
多谢,已经好了,原因是版本问题,你当时用的是7.0,如果大家有遇...
首先感谢搞出这么好的东东,可我使用的时候出现了点问题,希望能...
首先感谢搞出这么好的东东,可我使用的时候出现了点问题,希望能...
找到了,https://github.com/Wizzercn/NodeWk...
请问这个在哪下载?
大神竟然是合肥的,666
支持一下,分享的cms
http://sublime.nyasha.me/