JS string encoding conversion function

Source: Internet
Author: User

Escape method
Encode String objects so that they can be read on all computers,
Escape (charstring)
The required charstring parameter is any String object or literal that you want to encode.
Description
The Escape method returns a string value (Unicode format) that contains the contents of the charstring. All spaces,

Punctuation, accent marks, and other non-ASCII characters are replaced with%XX encoding, where xx equals 10 of the character

The number of six-in-system. For example, a space returns "%20". Characters with a value greater than 255 are stored in%UXXXX format.

Note: The escape method cannot be used to encode a Uniform Resource Identifier (URI). The encoding should be used

encodeURI and encodeURIComponent methods.

2.unescape method

Decodes a String object encoded with the escape method.
Unescape (charstring)
The required charstring parameter is the String object to decode.

Description
The Unescape method returns a string value containing the charstring content. All encoded in%XX 16

Characters are replaced with the medium-priced characters of the ASCII character set. Words encoded in%UXXXX format (Unicode characters)

Character is substituted with the Unicode character of the hexadecimal-encoded XXXX.
Note: The Unescape method cannot be used to decode a Uniform Resource Identifier (URI). The code can be solved using decodeURI and

decodeURIComponent method

When Ajax uses a GET request to pass a Chinese string, it must also encode the Chinese string into Unicode, typically using the JS

with function escape (). But we found a better function to make sure the functions of converting Chinese characters into Unicode encoding are 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,) >= 128)
{
Text = Text.replace (Matches[matchid], '%u00 ' + code);
}
}
}
Text = Text.replace ('%25 ', '%u0025 ');
return text;
}

The server side, of course, will have a second transcoding of the encoded string. Converts the string to UTF-8 encoding.
Copy code code as follows:

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, bits
Case ($intval & 0x7ff):
Return Chr (0xc0 | (($intval >> 6) & 0x1F)) .
Chr (0x80 | ($intval & 0x3F));
3 bytes Bits
Case ($intval & 0xFFFF):
Return Chr (0xe0 | (($intval >>) & 0x0f)) .
Chr (0x80 | (($intval >> 6) & 0x3F)) .
Chr (0x80 | ($intval & 0x3F));
4 bytes, bits
Case ($intval & 0X1FFFFF):
Return Chr (0xF0 | ($intval >> 18)) .
Chr (0x80 | (($intval >>) & 0x3F)) .
Chr (0x80 | (($intval >> 6) & 0x3F)) .
Chr (0x80 | ($intval & 0x3F));
}
}

This allows the Chinese string to be converted to UTF-8 encoding. This approach is suitable for various server environments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.