zoukankan      html  css  js  c++  java
  • 各种文字编码解码方式大合集

    统一的函数以下几种编码解码方式需要使用
    function left_zero_4(str) {
                if (str != null && str != '' && str != 'undefined') {
                    if (str.length == 2) {
                        return '00' + str;
                    }
                }
                return str;
            }
    中文汉字转Unicode
    function unicode(str){
                var value='';
                for (var i = 0; i < str.length; i++) {
                    value += '\u' + left_zero_4(parseInt(str.charCodeAt(i)).toString(16));
                }
                return value;
            }
            function left_zero_4(str) {
                if (str != null && str != '' && str != 'undefined') {
                    if (str.length == 2) {
                        return '00' + str;
                    }
                }
                return str;
            }
    Unicode转中文汉字、ASCII转换Unicode
    function reconvert(str){
                str = str.replace(/(\u)(w{1,4})/gi,function($0){
                    return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(w{1,4})/g,"$2")),16)));
                });
                str = str.replace(/(&#x)(w{1,4});/gi,function($0){
                    return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(w{1,4})(%3B)/g,"$2"),16));
                });
                str = str.replace(/(&#)(d{1,6});/gi,function($0){
                    return String.fromCharCode(parseInt(escape($0).replace(/(%26%23)(d{1,6})(%3B)/g,"$2")));
                });
                 
                return str;
            }
    Unicode转换ASCII
    function unicode1(str){
        var value='';
        for (var i = 0; i < str.length; i++)
            value += '&#' + str.charCodeAt(i) + ';';
        return value;
    }
    中文转换&#XXXX
    function ascii(str){
        var value='';
        for (var i = 0; i < str.length; i++) {
            value += '&#x' + left_zero_4(parseInt(str.charCodeAt(i)).toString(16))+';';
        }
        return value;
    }
     
  • 相关阅读:
    python CST中国标准时间格式转换
    pytest+报告插件
    getopt实现命令行
    初识Redis
    python list 切片及翻转的使用
    mysql中information_schema表
    获取两个字符串中最长的相同子串
    mongodb数据库备份
    VS2005下边不能使用target>remote tool解决方法
    wince LoadDriver tool
  • 原文地址:https://www.cnblogs.com/bluesky1024/p/7703995.html
Copyright © 2011-2022 走看看