Home  >  Article  >  Web Front-end  >  javascript unicode与GBK2312(中文)编码转换方法_基础知识

javascript unicode与GBK2312(中文)编码转换方法_基础知识

WBOY
WBOYOriginal
2016-05-16 17:15:171520browse

复制代码 代码如下:

var GB2312UnicodeConverter = {
    ToUnicode: function (str) {
        return escape(str).toLocaleLowerCase().replace(/%u/gi, '\\u');
    }
    , ToGB2312: function (str) {
        return unescape(str.replace(/\\u/gi, '%u'));
    }
};

var str = '上海', unicode;
document.write(str + '
');
unicode = GB2312UnicodeConverter.ToUnicode(str);
document.write('汉字转换为Unicode代码:' + unicode + '

');
document.write('Unicode代码转换为汉字:' + GB2312UnicodeConverter.ToGB2312(unicode));

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn