16 characters in JavaScript (improved) _javascript tips

Source: Internet
Author: User
The original code:
Copy Code code as follows:

Convert Text to 16
function Texttosix (str) {
return Escape (str). replace (/%/g, function () {
return "\";
}). toLowerCase ();
}

After the improvement:
Copy Code code as follows:

Convert Text to 16
function Texttohex (str) {
Escape to encode a Unicode value other than 0-255 by outputting the%u**** format, for example:%u6267%u624b%u6cea%u773c
Replace% of these with \ is hexadecimal
if (typeof str = = "string") {
return Escape (str). replace (/%/g, function () {
return "\";
}). toLowerCase ();
} else {
return null;
}
}

The main is to add a validation of whether the string is validated.

After perfecting this method, I also feel that we should improve the following method:
Copy Code code as follows:

To represent a 16 binary as text
function Sixtotext (str) {
return unescape (str);
}

The intention is to convert characters such as "\unnn" back to text. In fact, just look at the principle of unescape this method (how it works: by finding a sequence of characters in the form of%xx and%uxxxx (x as a hexadecimal number), the Unicode character \u00xx and \uxxxx replace such sequences of characters to decode. From the Baidu Encyclopedia explanation). You will know that I am completely superfluous. Because the browser is capable of parsing hexadecimal characters. For example:

In the IE9:

In Chrome:

Let's add another bit of harvest. In fact, in doing this parsing was replaced by my character, my original idea is to "\" This character is replaced by "%", and then use unescape this decoding function to parse the character. However, when I wanted to replace "\" with "%", I found the following interesting phenomenon: like "\" This character if not hit "T", "n" and so can be combined with t to form the character of the conversion, it will be ignored by the browser. Experiment to verify my conclusion:

Debugging in IE9:

To debug under Chrome:

The

Defines a variable here that contains some characters that may rarely appear in the variable "\", when debugging, look at the above hints, the browser is very smart to ignore the special position of the characters. So when you put some seemingly ordinary characters (such as file paths, etc.) in a JS variable, the result may not be expected.

Related Article

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.