String encoding question
What is the encoding of this string?
\x48\x65\x6C\x6C\x6F\x20
P.S. I found it in some Java开发者_StackOverflow中文版script source.
That is Hexadecimal for "Hello "
- 'H' is 48
- 'e' is 65
- 'l' is 6C
- 'o' is 6F
20 is a space
Here is a list of ascii codes: http://www.asciitable.com/
The \x
is just telling something that it is hexadecimal. In a Javascript string it will \x48
will represent a single character ('H'). x
is commonly used to mean hex.
For example, since 48 is Hexadecimal for the decimal number 72, the javascript statement:
0x48 === 72
is true
You can convert from Hex to ascii in javascript with String.fromCharCode(0x48)
.
You can convert from hexadecimal to decimal with parseInt('0x48')
Each of the values of the form \xNN
are hexadecimal literal values. They map to ASCII values encoding particular characters. If you go to a JavaScript console (for example, the one in the Chrome browser I'm using now) and enter
alert("\x48\x65\x6C\x6C\x6F\x20");
You'll get a popup that says
Hello
Since the ASCII values for the letters H, e, l, and o are 0x48, 0x65, 0x6C, and 0x6F, respectively. The final 0x20 encodes a space character, which does not display anywhere.
For more information about the ASCII table for encoding character values, see this website containing the complete table.
My question is why on earth any site would do this. It's less space-efficient in terms of number of source characters than just writing out Hello
!
That's hexadecimal for the string Hello
.
精彩评论