zoukankan      html  css  js  c++  java
  • 生成二维码的 jQuery 插件:jquery.qrcode.js的中文乱码问题

    在使用jquery.qrcode.js这个插件生成二维码的时候发现并不能识别中文。

    原因在于:jquery-qrcode是采用charCodeAt()方式进行编码转 换的。

    而这个方法默认会获取它的Unicode编码,如果有中文内容,在生成二维码前就要把字符串转换成UTF-8,然后再生成二维码。

    解决办法:

    通过以下函 数来转换中文字符串:

    function toUtf8(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;
            }

    然后再使用就可以了。

    var str = toUtf8("钓鱼岛是中国的!");   
    $('#code').qrcode(str); 
    唯有热爱方能抵御岁月漫长。
  • 相关阅读:
    POJ 3126 Prime Path
    POJ 2429 GCD & LCM Inverse
    POJ 2395 Out of Hay
    【Codeforces 105D】 Bag of mice
    【POJ 3071】 Football
    【POJ 2096】 Collecting Bugs
    【CQOI 2009】 余数之和
    【Codeforces 258E】 Devu and Flowers
    【SDOI 2010】 古代猪文
    【BZOJ 2982】 combination
  • 原文地址:https://www.cnblogs.com/syq816/p/8025087.html
Copyright © 2011-2022 走看看