Some time ago, I finally found a little introduction to JavaScript advanced programming:
\ Xnn represents a character in hexadecimal code nn (n: 0-F)
\ Unnn represents a Unicode Character in hexadecimal code (n: 0-F)
Today, we need to know the hexadecimal representation of some Chinese characters for a single requirement. Therefore, we have written two methods to convert the text into a hexadecimal representation, you can also convert hexadecimal code back to Chinese characters. The method is as follows:
Copy codeThe Code is as follows:
// Convert text to hexadecimal
Function textToSix (str ){
Return escape (str). replace (/%/g, function (){
Return "\\";
}). ToLowerCase ();
}
// Hexadecimal representation as text
Function sixToText (str ){
Return unescape (str );
}
Online Demo code:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <title> </title> <style type = "text/css"> table {border: 1px solid #12 aded; border-collapse: collapse;} table tr {border: 1px solid #12 aded; border-collapse: collapse;} table tr td {border: 1px solid #12 aded; border-collapse: collapse ;} </style> </pead> <body> <table cellpadding = "0" cellspacing = "0" border = "0px"> <tr> <td> <pre class =" code "id =" tbValue "rows =" 8 "cols =" 60 "> </textarea> </td> <input type =" button "value =" Set convert text to hexadecimal "onclick =" show () "/> </td> </tr> <td> <pre class =" code "id =" tbResult "rows =" 8 "cols =" 60 "> </textarea> </td> <input type = "button" value = "Convert hexadecimal to text" onclick = "show2 () "/> </td> </tr> </table> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]