Home  >  Article  >  Web Front-end  >  How to encode and convert javascript strings

How to encode and convert javascript strings

青灯夜游
青灯夜游Original
2021-04-19 15:14:579433browse

Methods for encoding and converting JavaScript strings: 1. Use the escape() function to encode strings so that they can be readable on all computers. The syntax is "escape (string)"; 2. Use unescape() function, syntax "unescape(string)".

How to encode and convert javascript strings

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

1. escape method

Encode String objects so that they can be readable on all computers,

escape(charString)

Required charstring parameter is to be encoded Any String object or literal.

Description:

The escape method returns a string value (Unicode format) containing the content of charstring. All spaces, punctuation, accents, and other non-ASCII characters are replaced with the %xx encoding, where xx is equal to the hexadecimal number representing the character. For example, spaces are returned as " ". Character values ​​greater than 255 are stored in %uxxxx format.

Note: The escape method cannot be used to encode Uniform Resource Identifiers (URIs). To encode it use the encodeURI and encodeURIComponent methods.

2. unescape method

Decode the String object encoded with the escape method.

unescape(charstring)

Required charstring parameter is the String object to be decoded.

Description:

The unescape method returns a string value containing the contents of charstring. All characters encoded in %xx hexadecimal form are replaced by equivalent characters in the ASCII character set. Characters encoded in the %uxxxx format (Unicode characters) are replaced with Unicode characters with hexadecimal encoding xxxx.

Note: The unescape method cannot be used to decode Uniform Resource Identifiers (URIs). To decode this code, you can use the decodeURI and decodeURIComponent methods

When AJAX uses GET requests to pass Chinese strings, the Chinese strings must also be encoded into unicode. Generally, the built-in function escape() of JS is used. However, it is found A better function to determine the conversion of Chinese characters into unicode encoding is as follows

function uniencode(text) 
{ 
text = escape(text.toString()).replace(/+/g, "%2B"); 
var matches = text.match(/(%([0-9A-F]{2}))/gi); 
if (matches) 
{ 
for (var matchid = 0; matchid < matches.length; matchid++) 
{ 
var code = matches[matchid].substring(1,3); 
if (parseInt(code, 16) >= 128) 
{ 
text = text.replace(matches[matchid], &#39;%u00&#39; + code); 
} 
} 
} 
text = text.replace(&#39;%25&#39;, &#39;%u0025&#39;); 
return text; 
}

Of course, the server side must perform a second transcoding on the encoded string. Convert the string into UTF-8 encoding .

function convert_int_to_utf8($intval) 
{ 
$intval = intval($intval); 
switch ($intval) 
{ 
// 1 byte, 7 bits 
case 0: 
return chr(0); 
case ($intval & 0x7F): 
return chr($intval); 
// 2 bytes, 11 bits 
case ($intval & 0x7FF): 
return chr(0xC0 | (($intval >> 6) & 0x1F)) . 
chr(0x80 | ($intval & 0x3F)); 
// 3 bytes, 16 bits 
case ($intval & 0xFFFF): 
return chr(0xE0 | (($intval >> 12) & 0x0F)) . 
chr(0x80 | (($intval >> 6) & 0x3F)) . 
chr (0x80 | ($intval & 0x3F)); 
// 4 bytes, 21 bits 
case ($intval & 0x1FFFFF): 
return chr(0xF0 | ($intval >> 18)) . 
chr(0x80 | (($intval >> 12) & 0x3F)) . 
chr(0x80 | (($intval >> 6) & 0x3F)) . 
chr(0x80 | ($intval & 0x3F)); 
} 
}

【Recommended learning: javascript advanced tutorial

The above is the detailed content of How to encode and convert javascript strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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