JAVA Escape/unescape

Source: Internet
Author: User

/** * JavaScript Escape/unescape encoded Java Implementation * Author Jackyz * Keep this copyright info while using the This method by free */ public class Escape {private final static string[] hex = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0        A "," 0B "," 0C "," 0D "," 0E "," 0F "," ten "," One "," a "," "", "" "," "", "" "," "," "", "1 A", "1 B", "1C", "1D", "1E", "1F", "A", "a", "a", "", "", "", "", "", "", "", "", "" 2A "," 2B "," 2C "," 2D "," 2E "," 2F "," 30 "," 31 "," 32 "," 33 "," 34 "," 35 ", "$", "Notoginseng", "a", "" "," 3 A "," 3B "," 3C "," 3D "," 3E "," 3F "," Max "," A "," a "," "", "" "," "," "," "," "," "," "," 4 A "," 4 B ", "4C", "4D", "4E", "4F", "60", "Wuyi", "si", "si", "WU", "" "," "", "" "," "", "" "," "5A", "5B", "5C", "5D", "5E", "5F", "", " "," "," "," "", "", "", "", "", "", "", "", "6A", "6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", " "," "," "," "7A", "7B", "7C", "7D", "7E", "7F", "the", "Bayi", "The" "" "," "", "" "," "," "," "," "," "," "," "," "8A" 8D "," 8E "," 8F "," 90 "," 91 "," 92 ",""94", "the", "", "" 98 "," A0 "," 9A "," 9B "," 9C "," 9D "," 9E "," 9F "," 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 "," D 4 "," D5 "," D6 "," D7 "," D8 "," D9 "," DA "," DB "," DC "," DD "," DE "," DF "," E0 "," E1 "," E2 "," E3 "," E4 "," E5 "," E6 "," E7 "," E8 "," E9 ","     EA "," EB "," EC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,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f, 0x3F,0x3F,0x3F,0 x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f, 0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f, 0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f, 0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,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.c                    Harat (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 = "Huang 1234 abcd[] () <+>,.~//";        System.out.println (stest);        System.out.println (Escape (stest));    System.out.println (Unescape (Escape (Stest))); }}

  

JAVA Escape/unescape

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.