zoukankan      html  css  js  c++  java
  • js计算字符串的字节数和字符串与二进制的相互转化

    一、js计算字符串的字节数方法:

    //blob获取字符串的字节
        var debug = "好的";
        var blob = new Blob([debug],{type : 'text/plain'});
        console.log(blob)
        /**
         * 计算字符串所占的内存字节数,默认使用UTF-8的编码方式计算,也可制定为UTF-16
         * UTF-8 是一种可变长度的 Unicode 编码格式,使用一至四个字节为每个字符编码
         * 
         * 000000 - 00007F(128个代码)      0zzzzzzz(00-7F)                             一个字节
         * 000080 - 0007FF(1920个代码)     110yyyyy(C0-DF) 10zzzzzz(80-BF)             两个字节
         * 000800 - 00D7FF 
           00E000 - 00FFFF(61440个代码)    1110xxxx(E0-EF) 10yyyyyy 10zzzzzz           三个字节
         * 010000 - 10FFFF(1048576个代码)  11110www(F0-F7) 10xxxxxx 10yyyyyy 10zzzzzz  四个字节
         * 
         * 注: Unicode在范围 D800-DFFF 中不存在任何字符
         * {@link http://zh.wikipedia.org/wiki/UTF-8}
         * 
         * UTF-16 大部分使用两个字节编码,编码超出 65535 的使用四个字节
         * 000000 - 00FFFF  两个字节
         * 010000 - 10FFFF  四个字节
         * 
         * {@link http://zh.wikipedia.org/wiki/UTF-16}
         * @param  {String} str 
         * @param  {String} charset utf-8, utf-16
         * @return {Number}
         */
        var sizeof = function(str, charset){
            var total = 0,
                charCode,
                i,
                len;
            charset = charset ? charset.toLowerCase() : '';
            if(charset === 'utf-16' || charset === 'utf16'){
                for(i = 0, len = str.length; i < len; i++){
                    charCode = str.charCodeAt(i);
                    if(charCode <= 0xffff){
                        total += 2;
                    }else{
                        total += 4;
                    }
                }
            }else{
                for(i = 0, len = str.length; i < len; i++){
                    charCode = str.charCodeAt(i);
                    if(charCode <= 0x007f) {
                        total += 1;
                    }else if(charCode <= 0x07ff){
                        total += 2;
                    }else if(charCode <= 0xffff){
                        total += 3;
                    }else{
                        total += 4;
                    }
                }
            }
            return total;
        }
        console.log(sizeof(debug))

    二、字符串与二进制的相互转化:

    //字符串转ascii码,用charCodeAt();
    //ascii码转字符串,用fromCharCode();
    var str = "A";
    var code = str.charCodeAt();
    var str2 = String.fromCharCode(code);
    
    十进制转二进制
    
    var a = "i";
    console.log(a.charCodeAt()); //105
    console.log(a.charCodeAt().toString(2)); //1101001
    
    //将字符串转换成二进制形式,中间用空格隔开
    function strToBinary(str){
        var result = [];
        var list = str.split("");
        for(var i=0;i<list.length;i++){
            if(i != 0){
                result.push(" ");
            }
            var item = list[i];
            var binaryStr = item.charCodeAt().toString(2);
            result.push(binaryStr);
        }   
        return result.join("");
    }
     
    console.log(strToBinary("我们二进制数据y_+=ha好的")); //110001000010001 100111011101100 100111010001100 1000111111011011 101001000110110 110010101110000 110001101101110 1111001 1011111 101011 111101 1101000 1100001 101100101111101 111011010000100
    console.log(strToBinary("@%$+测试{'obj':'124'}")); //1000000 100101 100100 101011 110110101001011 1000101111010101 1111011 100111 1101111 1100010 1101010 100111 111010 100111 110001 110010 110100 100111 1111101
     
    //将二进制字符串转换成Unicode字符串
    function binaryToStr(str){
        var result = [];
        var list = str.split(" ");
        for(var i=0;i<list.length;i++){
             var item = list[i];
             var asciiCode = parseInt(item,2);
             var charValue = String.fromCharCode(asciiCode);
             result.push(charValue);
        }
        return result.join("");
    }
     
    console.log(binaryToStr("110001000010001 100111011101100 100111010001100 1000111111011011 101001000110110 110010101110000 110001101101110 1111001 1011111 101011 111101 1101000 1100001 101100101111101 111011010000100")); //我们二进制数据y_+=ha好的
    console.log(binaryToStr("1000000 100101 100100 101011 110110101001011 1000101111010101 1111011 100111 1101111 1100010 1101010 100111 111010 100111 110001 110010 110100 100111 1111101")); //@%$+测试{'obj':'124'}
    
  • 相关阅读:
    【SQL】行转列
    【SQL】高级函数汇总
    VisualStudio优秀扩展插件推荐
    【SQL】多行转一列 FOR XML PATH
    【SQL】判断一个字符串是否在另外一个字符串中
    【NodeJS】Windows环境初始化
    【WPF】实现加载中动画效果
    C# ffmpeg简单帮助类
    ZSH出现问题
    Manjaro 安装后的配置
  • 原文地址:https://www.cnblogs.com/dearxinli/p/7657512.html
Copyright © 2011-2022 走看看