js小工具.字符串&16进制数值字符串&互转

Posted h5skill

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js小工具.字符串&16进制数值字符串&互转相关的知识,希望对你有一定的参考价值。

1、

2、测试代码:

<!DOCTYPE html>

<html>
<head>
    <meta charset="UTF-8">

    <script type="text/javascript" >
    <!--

        window.onload = function()
        {
        //    var strTest = "1234567";
        //    strTest = strTest.substr(2);
        //    console.log("strTest : " + strTest);

        //    var str ="GET / HTTP/1.1
"
        //            +"Host: www.google.com
"
        //            +"User-Agent: curl/7.43.0
"
        //            +"Accept: */*
"
        //            +"
";
            var str ="GET / HTTP/1.1
"
                    +"Host: www.baidu.com
"
                    +"User-Agent: curl/7.43.0
"
                    +"Accept: */*
"
                    +"
";
            var strHex = strToHexCharCode(str);
            console.log(str); 
            console.log("字符串 转为 16进制数值的字符串:
"); 
            console.log(strHex);

            console.log("");
        //    strHex = "474554202f20485454502f312e310d0a486f73743a"
        //            +"207777772e676f6f676c652e636f6d0d0a55736572"
        //            +"2d4167656e743a206375726c2f372e34332e300d0a"
        //            +"4163636570743a202a2f2a0d0a0d0a";
            strHex = "474554202f20485454502f312e310d0a486f73743a207777772e62616964752e636f6d0d0a557365722d4167656e743a206375726c2f372e34332e300d0a4163636570743a202a2f2a0d0a0d0a";
            console.log(strHex);

            console.log("16进制数值的字符串 转为 字符串:
"); 
            str = hexCharCodeToStr(strHex);
            console.log(str);
        };

        //字符串转16进制
        function strToHexCharCode(_str)
        {
            if(_str === "")
                return "";
            var hexCharCode = [];
            hexCharCode.push("0x
"); 
            for(var i = 0; i < _str.length; i++) {
                var str = _str.charCodeAt(i).toString(16);
                str = LeftPad_2(str);
                hexCharCode.push(str);
            }
            return hexCharCode.join("");
        }


        // 16进制转字符串
        function hexCharCodeToStr(_hexCharCodeStr)
        {
            var trimedStr = _hexCharCodeStr.trim();
            var rawStr = trimedStr.substr(0,2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
            var len = rawStr.length;
            if(len % 2 !== 0) {
                alert("Illegal Format ASCII Code!");
                return "";
            }
            var curCharCode;
            var resultStr = [];
            for(var i = 0; i < len;i = i + 2) {
                curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
                resultStr.push(String.fromCharCode(curCharCode));
            }
            return resultStr.join("");
        }

        function LeftPad_2(_str)
        {
            if (_str.length == 1)
                return "0"+_str;
            return _str;
        }

    -->
    </script>

</head>

<body>



</body>
</html>

 

3、

4、

5、

 

以上是关于js小工具.字符串&16进制数值字符串&互转的主要内容,如果未能解决你的问题,请参考以下文章

qt怎样将字符串转化为16进制

ColorUtilColor工具类(color整型rgb数组16进制互相转换)

16进制字符串如何转化16进制数值

labview 16进制字符串转换成10进制数值

在labview中如何将16进制字符串数组转化为十进制数值数组?

lua 转换16进制字符串为10进制数值