Convert special characters from cookies

Source: Internet
Author: User
The values in the cookie stored on the page contain special characters, but the passed values contain some special characters such as ":", which cannot be directly carried by special characters. Therefore, when storing cookies, convert it to unicode encoding:

Document. cookie = Key + "=" + escape (value); however, if it is obtained directly in the action, the Unicode encoding of special characters cannot be converted to the desired one. Search the relevant information online, find the Unicode Conversion Tool class written in Java. The escape in the project is the tool class, and the string code = escape is used in the action. unescape (Cookies [I]. getvalue ());

After the method is converted, Unicode can be converted to the special character I want.

The source code of escape. Java is as follows:

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, 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 };

/**
* <P> discription: [Add code] </P>
* @ Param s
* @ Return
* @ Author: [XU Di]
* @ Update: [date YYYY-MM-DD] [modifier name] [Change description]
*/
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 ();
}

/**
* <P> discription: [decoding] </P>
* @ Param s
* @ Return
* @ Author: [XU Di]
* @ Update: [date YYYY-MM-DD] [modifier name] [Change description]
*/
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) | escape. Val [S. charat (I + 1)];
CINT = (CINT <4) | escape. Val [S. charat (I + 2)];
I + = 2;
}
Else
{// % Uxxxx: map to Unicode (XXXX)
CINT = (CINT <4) | escape. Val [S. charat (I + 2)];
CINT = (CINT <4) | escape. Val [S. charat (I + 3)];
CINT = (CINT <4) | escape. Val [S. charat (I + 4)];
CINT = (CINT <4) | escape. Val [S. charat (I + 5)];
I + = 5;
}
Sbuf. append (char) CINT );
}
I ++;
}
Return sbuf. tostring ();
}

}

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.