JAVA Implementation of JavaScript escape/Unescape Encoding

Source: Internet
Author: User
Java escape code that complies with JavaScript Unescape functions (supports Unicode). Recently, a cross-origin interaction program has been created. The interaction process is very complex. In addition, one end can only use JavaScript to process the returned results.

Therefore, we found that the urlencoder. encode method of Java is no longer compatible with the Unescape method of JavaScript.

In the mainstream browser versions, JavaScript has begun to use Unicode as the string's internal code for encoding. that is, the Unicode Character After escape looks like % uabcd and Java's urlencoder. the result obtained by the encode method is % AB % Cd. The converted result must be garbled.

There are two methods. One is to use Java to generate a new encoding method. second, use JavaScript to implement a decoding method for the old encoding. I thought about it. It is a little more convenient to use Java.

The JAVA Implementation of the escape posted last time seems to be useful to everyone. Recently, Zeng Jun proposed that Unescape also makes sense for the interaction between JavaScript and the backend, so it was implemented together.

The code is very simple, so there is no need to comment. Consistently, we still follow the principle of "Change Time by space.

The required bucket. Always, help yourself.

/**
* JAVA Implementation of JavaScript escape/Unescape Encoding
* Author jackyz
* Keep this copyright info while using this method by free
*/
Public class escape {
Private Final Static string [] hex = {
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09 ", "0a", "0b", "0C", "0d", "0e", "0f ",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19 ", "1A", "1B", "1C", "1D", "1E", "1f ",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29 ", "2a", "2B", "2C", "2D", "2e", "2f ",
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39 ", "3A", "3B", "3C", "3D", "3e", "3f ",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49 ", "4A", "4B", "4C", "4D", "4E", "4f ",
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59 ", "5A", "5B", "5c", "5d", "5E", "5f ",
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69 ", "6a", "6B", "6C", "6D", "6e", "6f ",
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79 ", "7A", "7b", "7C", "7D", "7E", "7f ",
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89 ", "8a", "8b", "8C", "8d", "8e", "8f ",
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99 ", "9A", "9B", "9C", "9d", "9e", "9f ",
"A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9 ", "AA", "AB", "AC", "ad", "AE", "af ",
"B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9 ", "Ba", "BB", "BC", "BD", "be", "BF ",
"C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9 ", "ca", "CB", "cc", "cd", "CE", "CF ",
"D0", "d1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9 ", "Da", "DB", "DC", "DD", "de", "DF ",
"E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9 ", "EA", "Eb", "EC", "Ed", "ee", "Ef ",
"F0", "f1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9 ", "Fa", "FB", "FC", "FD", "Fe", "FF"
};
Private Final Static byte [] val = {
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0 x, 0 x, 0 x, 0 x, 0 x, 0x09, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x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
Stringbuffer sbuf = new stringbuffer ();
Int Len = S. Length ();
For (INT I = 0; I <Len; I ++ ){
Int CH = S. charat (I );
If (CH = '') {// space: map to '+'
Sbuf. append ('+ ');
} Else if ('A' <= CH & Ch <= 'Z') {// 'A' .. 'Z': as it was
Sbuf. append (char) CH );
} Else if ('A' <= CH & Ch <= 'Z') {// 'A' .. 'Z': as it was
Sbuf. append (char) CH );
} Else if ('0' <= CH & Ch <= '9') {// '0' .. '9': as it was
Sbuf. append (char) CH );
} Else if (CH = '-' | CH = '_' // unreserved: as it was
| CH = '.' | CH = '! '
| CH = '~ '| CH = '*'
| CH = '/''| CH = '('
| CH = ')'){
Sbuf. append (char) CH );
} Else if (CH <= 0x007f) {// other ASCII: map to % xx
Sbuf. append ('% ');
Sbuf. append (hex [CH]);
} Else {// UNICODE: map to % uxxxx
Sbuf. append ('% ');
Sbuf. append ('U ');
Sbuf. append (hex [(CH >>> 8)]);
Sbuf. append (hex [(0x00ff & Ch)]);
}
}
Return sbuf. tostring ();
}
Public static string Unescape (string s ){
Stringbuffer sbuf = new stringbuffer ();
Int I = 0;
Int Len = S. Length ();
While (I <Len ){
Int CH = S. charat (I );
If (CH = '+') {// +: map''
Sbuf. append ('');
} Else if ('A' <= CH & Ch <= 'Z') {// 'A' .. 'Z': as it was
Sbuf. append (char) CH );
} Else if ('A' <= CH & Ch <= 'Z') {// 'A' .. 'Z': as it was
Sbuf. append (char) CH );
} Else if ('0' <= CH & Ch <= '9') {// '0' .. '9': as it was
Sbuf. append (char) CH );
} Else if (CH = '-' | CH = '_' // unreserved: as it was
| CH = '.' | CH = '! '
| CH = '~ '| CH = '*'
| CH = '/''| CH = '('
| CH = ')'){
Sbuf. append (char) CH );
} Else if (CH = '% '){
Int CINT = 0;
If ('U '! = S. charat (I + 1) {// % XX: map to ASCII (XX)
CINT = (CINT <4) | Val [S. charat (I + 1)];
CINT = (CINT <4) | Val [S. charat (I + 2)];
I + = 2;
} Else {// % uxxxx: map to Unicode (XXXX)
CINT = (CINT <4) | Val [S. charat (I + 2)];
CINT = (CINT <4) | Val [S. charat (I + 3)];
CINT = (CINT <4) | Val [S. charat (I + 4)];
CINT = (CINT <4) | Val [S. charat (I + 5)];
I + = 5;
}
Sbuf. append (char) CINT );
}
I ++;
}
Return sbuf. tostring ();
}
Public static void main (string [] ARGs ){
String stest = "Chinese 1234 ABCD [] () <+> ,.~ //";
System. Out. println (stest );
System. Out. println (escape (stest ));
System. Out. println (Unescape (escape (stest )));
}
}

 

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.