问答详情
源自:6-4 使用QR Code方式生成和解析二维码

中文乱码怎么改?

我自己写的中文无论怎么改为啥都是乱码呢?请问各路大牛

提问者:慕雪8357630 2017-05-09 16:36

个回答

  • 只要我想
    2017-05-10 10:53:01

    在二维码编码前把字符串转换成UTF-8

    function utf16to8(str) {

        var out, i, len, c;

        out = "";

        len = str.length;

        for(i = 0; i < len; i++) {

    c = str.charCodeAt(i);

    if ((c >= 0x0001) && (c <= 0x007F)) {

       out += str.charAt(i);

    } else if (c > 0x07FF) {

       out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));

       out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));

       out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));

    } else {

       out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));

       out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));

    }

        }

        return out;

    }