欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js实现unicode码字符串与utf8字节数据互转

程序员文章站 2022-04-23 08:17:26
js的string变量存储字符串使用的是unicode编码,要保存时必须选择其他编码后进行传输,比如转成utf-8,utf-32等。存储到数据库中为utf-8编码,读取出来如何转换成正确的字符串就成了问题。现在给出解决方案,可以正确支持中文、emoji表情、英文混合的字符串编码互转。 针对emoji ......

js的string变量存储字符串使用的是unicode编码,要保存时必须选择其他编码后进行传输,比如转成utf-8,utf-32等。存储到数据库中为utf-8编码,读取出来如何转换成正确的字符串就成了问题。现在给出解决方案,可以正确支持中文、emoji表情、英文混合的字符串编码互转。

 

/**
 * created by hdwang on 2019/1/28.
 */
var convertutf8 = (function() {

    /**
     * unicode string to utf-8
     * @param text 字符串
     * @returns {*} utf-8编码
     */
    function tobytes(text) {
        var result = [], i = 0;
        text = encodeuri(text);
        while (i < text.length) {
            var c = text.charcodeat(i++);

            // if it is a % sign, encode the following 2 bytes as a hex value
            if (c === 37) {
                result.push(parseint(text.substr(i, 2), 16))
                i += 2;

                // otherwise, just the actual byte
            } else {
                result.push(c)
            }
        }

        return coercearray(result);
    }


    /**
     * utf8 byte to unicode string
     * @param utf8bytes
     * @returns {string}
     */
    function utf8bytetounicodestr(utf8bytes){
        var unicodestr ="";
        for (var pos = 0; pos < utf8bytes.length;){
            var flag= utf8bytes[pos];
            var unicode = 0 ;
            if ((flag >>>7) === 0 ) {
                unicodestr+= string.fromcharcode(utf8bytes[pos]);
                pos += 1;

            } else if ((flag &0xfc) === 0xfc ){
                unicode = (utf8bytes[pos] & 0x3) << 30;
                unicode |= (utf8bytes[pos+1] & 0x3f) << 24;
                unicode |= (utf8bytes[pos+2] & 0x3f) << 18;
                unicode |= (utf8bytes[pos+3] & 0x3f) << 12;
                unicode |= (utf8bytes[pos+4] & 0x3f) << 6;
                unicode |= (utf8bytes[pos+5] & 0x3f);
                unicodestr+= string.fromcodepoint(unicode) ;
                pos += 6;

            }else if ((flag &0xf8) === 0xf8 ){
                unicode = (utf8bytes[pos] & 0x7) << 24;
                unicode |= (utf8bytes[pos+1] & 0x3f) << 18;
                unicode |= (utf8bytes[pos+2] & 0x3f) << 12;
                unicode |= (utf8bytes[pos+3] & 0x3f) << 6;
                unicode |= (utf8bytes[pos+4] & 0x3f);
                unicodestr+= string.fromcodepoint(unicode) ;
                pos += 5;

            } else if ((flag &0xf0) === 0xf0 ){
                unicode = (utf8bytes[pos] & 0xf) << 18;
                unicode |= (utf8bytes[pos+1] & 0x3f) << 12;
                unicode |= (utf8bytes[pos+2] & 0x3f) << 6;
                unicode |= (utf8bytes[pos+3] & 0x3f);
                unicodestr+= string.fromcodepoint(unicode) ;
                pos += 4;

            } else if ((flag &0xe0) === 0xe0 ){
                unicode = (utf8bytes[pos] & 0x1f) << 12;;
                unicode |= (utf8bytes[pos+1] & 0x3f) << 6;
                unicode |= (utf8bytes[pos+2] & 0x3f);
                unicodestr+= string.fromcharcode(unicode) ;
                pos += 3;

            } else if ((flag &0xc0) === 0xc0 ){ //110
                unicode = (utf8bytes[pos] & 0x3f) << 6;
                unicode |= (utf8bytes[pos+1] & 0x3f);
                unicodestr+= string.fromcharcode(unicode) ;
                pos += 2;

            } else{
                unicodestr+= string.fromcharcode(utf8bytes[pos]);
                pos += 1;
            }
        }
        return unicodestr;
    }



    function checkint(value) {
        return (parseint(value) === value);
    }

    function checkints(arrayish) {
        if (!checkint(arrayish.length)) { return false; }

        for (var i = 0; i < arrayish.length; i++) {
            if (!checkint(arrayish[i]) || arrayish[i] < 0 || arrayish[i] > 255) {
                return false;
            }
        }

        return true;
    }

    function coercearray(arg, copy) {

        // arraybuffer view
        if (arg.buffer && arg.name === 'uint8array') {

            if (copy) {
                if (arg.slice) {
                    arg = arg.slice();
                } else {
                    arg = array.prototype.slice.call(arg);
                }
            }

            return arg;
        }

        // it's an array; check it is a valid representation of a byte
        if (array.isarray(arg)) {
            if (!checkints(arg)) {
                throw new error('array contains invalid value: ' + arg);
            }

            return new uint8array(arg);
        }

        // something else, but behaves like an array (maybe a buffer? arguments?)
        if (checkint(arg.length) && checkints(arg)) {
            return new uint8array(arg);
        }

        throw new error('unsupported array-like object');
    }

    return {
        tobytes: tobytes,
        frombytes: utf8bytetounicodestr
    }
})()

针对emoji的字节字符,占两个unicode字符。使用string.fromcharcode也可以实现,需要进行两次fromcharcode,没有frompointcode方便。下面展示了utf-8的4字节转换为unicode(utf-16)的过程。

//高char10位[一个unicode字符] (2+6+2=10)
unicode =   ((utf8bytes[pos] & 0x3)) << 8 |((utf8bytes[pos+1] & 0x3f) << 2) |((utf8bytes[pos+2] >> 4) & 0x03);

//减去‭1f600‬中的1,这里减去6个0即可,低位char已经占据10位
unicode = unicode - parseint('1000000',2)

//加上utf-16高char的标识符
unicode =  0xd800 + unicode;
console.log(unicode);
unicodestr +=  string.fromcharcode(unicode);

//低char10位[一个unicode字符](4+6)
unicode =  ((utf8bytes[pos+2] & 0x0f) << 6) | (utf8bytes[pos+3] & 0x3f);
//加上utf-16低char的标识符
unicode = 0xdc00 + unicode;
console.log(unicode);
unicodestr+=  string.fromcharcode(unicode);
pos += 4;

 

 

参考链接: