• JS字符串与二进制的相互转化


    1
    2
    3
    4
    5
    //字符串转ascii码,用charCodeAt();
    //ascii码转字符串,用fromCharCode();
    var str = "A";
    var code = str.charCodeAt();
    var str2 = String.fromCharCode(code);

     十进制转二进制

    1
    2
    3
    var a = "i";
    console.log(a.charCodeAt()); //105
    console.log(a.charCodeAt().toString(2)); //1101001
    1
    2
    3
    var a = "我";
    console.log(a.charCodeAt()); //25105
    console.log(a.charCodeAt().toString(2)); //110001000010001
    1
    2
    3
    4
    var a = "我们";
    console.log(a.length); //2
    var list = a.split("");
    console.log(list.length); //2<br>console.log(a.charCodeAt().toString(2)); //110001000010001 100111011101100
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    //将字符串转换成二进制形式,中间用空格隔开
    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("我们")); //110001000010001 100111011101100
    console.log(strToBinary("@%$+")); //1000000 100101 100100 101011
     
    //将二进制字符串转换成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")); //我们
    console.log(binaryToStr("1000000 100101 100100 101011")); //@%$+ 
     
     
    const left_pad = (str, len, ch)  => {//补全左边
        str = str + '';
        len = len - str.length;
        if (len <= 0) return str;
    
        if (!ch && ch !== 0) ch = ' ';
        ch = ch + '';
        var pad = '';
        
        while (true) {
            if (len & 1) pad += ch;
            len >>= 1;
            if (len) ch += ch;
            else break;
        }
        return pad + str;
    }
    left_pad('1011', 8, '0') //00001011

      参考出处:http://www.cnblogs.com/it-deepinmind/

      

    1
    2
    3
    4
    5
    //字符串转ascii码,用charCodeAt();
    //ascii码转字符串,用fromCharCode();
    var str = "A";
    var code = str.charCodeAt();
    var str2 = String.fromCharCode(code);

      十进制转二进制

    1
    2
    3
    var a = "i";
    console.log(a.charCodeAt()); //105
    console.log(a.charCodeAt().toString(2)); //1101001

      

    1
    2
    3
    var a = "我";
    console.log(a.charCodeAt()); //25105
    console.log(a.charCodeAt().toString(2)); //110001000010001

      

    1
    2
    3
    4
    var a = "我们";
    console.log(a.length); //2
    var list = a.split("");
    console.log(list.length); //2<br>console.log(a.charCodeAt().toString(2)); //110001000010001 100111011101100

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    //将字符串转换成二进制形式,中间用空格隔开
    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(binartStr);
        }   
        return result.join("");
    }
     
    console.log(strToBinary("我们")); //110001000010001 100111011101100
    console.log(strToBinary("@%$+")); //1000000 100101 100100 101011
     
    //将二进制字符串转换成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")); //我们
    console.log(binaryToStr("1000000 100101 100100 101011")); //@%$+ 
  • 相关阅读:
    (转)Esri微博地址收录
    使用liquibasemavenplugin实现持续数据库集成
    CenOS系统中安装Tomcat7并设置为自启动服务
    Androidx86虚拟机安装配置全攻略
    jdk8根据某一属性去重方法 Collectors.collectingAndThen
    [转]恶心的C语言strtok函数
    人生七日忠告 人生征途须记
    VTK中二维Delaunay三角剖分
    Python编程题41原地反转字符串
    Python编程题42除自身以外元素的乘积
  • 原文地址:https://www.cnblogs.com/zhu573514187/p/13202990.html
Copyright © 2020-2023  润新知