js把2字节unicode编码转换成utf8编码

    /* 作者:狡猾的皮球
    * 2019年12月10日 16:33:04
    * 功能:
    *   2字节unicode转换成1~4字节的utf8字节数组
    * 参数:
    *   unic:0~65535之间的整数
    * 返回值:
    *   1~4字节的整型数组,比如[228,189,160]或者[64]等等 */
    function unicode2utf8(unic) {
        var bytes = [];
        if (unic <= 0x0000007F) {            // * U-00000000 - U-0000007F:  0xxxxxxx
            bytes[0] = (unic & 0x7F);
        } else if (unic >= 0x00000080 && unic <= 0x000007FF) {            // * U-00000080 - U-000007FF:  110xxxxx 10xxxxxx
            bytes[1] = (unic & 0x3F) | 0x80;
            bytes[0] = ((unic >> 6) & 0x1F) | 0xC0;
        } else if (unic >= 0x00000800 && unic <= 0x0000FFFF) {            // * U-00000800 - U-0000FFFF:  1110xxxx 10xxxxxx 10xxxxxx
            bytes[2] = (unic & 0x3F) | 0x80;
            bytes[1] = ((unic >> 6) & 0x3F) | 0x80;
            bytes[0] = ((unic >> 12) & 0x0F) | 0xE0;
        } else if (unic >= 0x00010000 && unic <= 0x001FFFFF) {            // * U-00010000 - U-001FFFFF:  11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
            bytes[3] = (unic & 0x3F) | 0x80;
            bytes[2] = ((unic >> 6) & 0x3F) | 0x80;
            bytes[1] = ((unic >> 12) & 0x3F) | 0x80;
            bytes[0] = ((unic >> 18) & 0x07) | 0xF0;
        } else if (unic >= 0x00200000 && unic <= 0x03FFFFFF) {            // * U-00200000 - U-03FFFFFF:  111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
            bytes[4] = (unic & 0x3F) | 0x80;
            bytes[3] = ((unic >> 6) & 0x3F) | 0x80;
            bytes[2] = ((unic >> 12) & 0x3F) | 0x80;
            bytes[1] = ((unic >> 18) & 0x3F) | 0x80;
            bytes[0] = ((unic >> 24) & 0x03) | 0xF8;
        } else if (unic >= 0x04000000 && unic <= 0x7FFFFFFF) {            // * U-04000000 - U-7FFFFFFF:  1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
            bytes[5] = (unic & 0x3F) | 0x80;
            bytes[4] = ((unic >> 6) & 0x3F) | 0x80;
            bytes[3] = ((unic >> 12) & 0x3F) | 0x80;
            bytes[2] = ((unic >> 18) & 0x3F) | 0x80;
            bytes[1] = ((unic >> 24) & 0x3F) | 0x80;
            bytes[0] = ((unic >> 30) & 0x01) | 0xFC;
        }
        return bytes;
    }
发布了42 篇原创文章 · 获赞 112 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/qq_39687901/article/details/103480610
今日推荐