Home > Web Front-end > JS Tutorial > body text

Conversion method between string and hexadecimal string represented by javascript_javascript skills

WBOY
Release: 2016-05-16 15:49:43
Original
1345 people have browsed it

The example in this article describes the mutual conversion method between string and hexadecimal representation string implemented by javascript. Share it with everyone for your reference. The details are as follows:

The reason why I wrote this is because I found that strings represented by hexadecimal are often used in SQL injection and XSS, such as
SELECT CONCAT(0x68656c6c6f);
What I got was hello

<!DOCTYPE html>
<html>
 <head>
  <title>Hex-Char Bi-Converter</title>
 </head>
 <body>
  <div class="mainContainer">
   <label for="from" id="fromLabel">String</label>
   <input type="text" name="from" id="from" />
   <input type="button" name="exchange" id="exchange" value="<=>" />
   <label for="to" id="toLabel">Hex</label>
   <input type="text" name="to" id="to" />
   <input type="button" name="convert" id="convert" value="Convert" />
  </div>
  <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
  <script type="text/javascript">
   var curMode = 0; // curMode: 0 represents String to Hex, 1 from Hex to String
   var fromToLabelArray = ["Hex", "String"];
   $(function() {
    $("#convert").click(function() {
     var fromVal = $("#from").val();
     var toVal = 
      curMode === 0 
      &#63; 
      strToHexCharCode(fromVal) 
      :
      hexCharCodeToStr(fromVal);
     $("#to").val(toVal);
    });
    $("#exchange").click(function() {
     $("#fromLabel").text(fromToLabelArray[curMode]);
     $("#toLabel").text(fromToLabelArray[1-curMode]);
     curMode = 1 - curMode;
    });
   });
   function strToHexCharCode(str) {
    if(str === "")
     return "";
    var hexCharCode = [];
    hexCharCode.push("0x");    
    for(var i = 0; i < str.length; i++) {
     hexCharCode.push((str.charCodeAt(i)).toString(16));
    }
    return hexCharCode.join("");
   }
   function hexCharCodeToStr(hexCharCodeStr) {
    var trimedStr = hexCharCodeStr.trim();
    var rawStr = 
      trimedStr.substr(0,2).toLowerCase() === "0x"
      &#63; 
      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("");
   }
  </script>
 </body>
</html>

Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!