Javascript| encoding Java escape encoding conforming to JavaScript unescape functions (Unicode enabled)
The interaction process is very complex when you are working on a cross-domain interaction. Also, one end can only use JavaScript to process the return.
As a result, discovering Java's urlencoder.encode approach is no longer compatible with JavaScript's unescape approach.
JavaScript is now encoded in the mainstream version of the browser using Unicode as the inner code of the string. That is, the Unicode character after escape looks like the result of the%UABCD and Java Urlencoder.encode method is%AB%CD, the result of conversion must be garbled.
There are two methods, one of which is to implement a method of generating a new encoding in Java. Second, using JavaScript to implement a decoding method for the old encoding. Think about it, or use Java to facilitate a little bit.
The Java implementation of the escape that was posted last time seems to be useful to everyone. Recently Zeng Jun students suggested that Unescape is also meaningful for JavaScript and back-end interactions. And then it came true.
The code is very simple and the comment is free. Consistent with the principle of "space for Time" is still followed.
Need the bucket, consistent, help yourself.
/**
* JavaScript Escape/unescape coded Java implementation
* Author Jackyz
* Keep this Copyright
*/
public class Escape {
Private final static string[] hex = {
"," "," "," "," "", "" "," "", "" "," "", "" "," "", "" "," "0A", "0B", "0C", "0D", "0E", "0F",
"Ten", "One", "", "", "," "," "," "," "," "," "1 A", "1 B", "1C", "1D", "1E", "1F",
"", "", "", "", "", "", "", "", "", "", "", "", "" 2A "," 2B "," 2C "," 2D "," 2E "," 2F ",
"The", "the", "", "" "," "," "," "," "," "," "," "3", "the", "3B", "3C", "3D", "3E", "3F",
"The", "a", "", "," "," "," "," "," "," "," "," "4 A", "4 B", "4C", "4D", "4E", "4F",
"A", "" "," "", "", "", "", "", "", "", "", "", "" "", "5A", "5B", "5C", "5D", "5E", "5F",
"A", "", "" "," "," "," "," "," "," "," "," "," "," "6A", "6B", "6C", "6D", "6E", "6F",
"", "", "" "," "," "," "," "," "," "," "," "", "", "", "" 7A "," 7B "," 7C "," 7D "," 7E "," 7F ",
"No", "I", "" "," "," "," "," "," "," "", "", "", "" "," "8A", "8B", "8C", "8D", "8E", "8F",
"", "", "" "," "," "," "," "," "," "", "" "," "", "" "," 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,
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,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,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,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,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,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
};
public static String Escape (string s) {
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 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 = = '% ') {
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));
}
}