Java implementation of JavaScript escape/unescape encoding
Source: Internet
Author: User
Java escape coding implementation that complies with JavaScript unescape functions (Unicode supported)
Recently, a cross-origin interaction program is being created, and the interaction process is very complicated. 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 ",
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.