SoFunction
Updated on 2025-03-08

Example of url Chinese character encoding conversion between each other in Java

//Convert %E4%BD%A0 to Chinese characters
    public static String unescape(String s) {
        StringBuffer sbuf = new StringBuffer();
        int l = ();
        int ch = -1;
        int b, sumb = 0;
        for (int i = 0, more = -1; i < l; i++) {
            /* Get next byte b from URL segment s */
            switch (ch = (i)) {
            case '%':
                ch = (++i);
                int hb = (((char) ch) ? ch - '0'
                        : 10 + ((char) ch) - 'a') & 0xF;
                ch = (++i);
                int lb = (((char) ch) ? ch - '0'
                        : 10 + ((char) ch) - 'a') & 0xF;
                b = (hb << 4) | lb;
                break;
            case '+':
                b = ' ';
                break;
            default:
                b = ch;
            }
            /* Decode byte b as UTF-8, sumb collects incomplete chars */
            if ((b & 0xc0) == 0x80) { // 10xxxxxx (continuation byte)  
                sumb = (sumb << 6) | (b & 0x3f); // Add 6 bits to sumb  
                if (--more == 0)
                    ((char) sumb); // Add char to sbuf  
            } else if ((b & 0x80) == 0x00) { // 0xxxxxxx (yields 7 bits)  
                ((char) b); // Store in sbuf  
            } else if ((b & 0xe0) == 0xc0) { // 110xxxxx (yields 5 bits)  
                sumb = b & 0x1f;
                more = 1; // Expect 1 more byte  
            } else if ((b & 0xf0) == 0xe0) { // 1110xxxx (yields 4 bits)  
                sumb = b & 0x0f;
                more = 2; // Expect 2 more bytes  
            } else if ((b & 0xf8) == 0xf0) { // 11110xxx (yields 3 bits)  
                sumb = b & 0x07;
                more = 3; // Expect 3 more bytes  
            } else if ((b & 0xfc) == 0xf8) { // 111110xx (yields 2 bits)  
                sumb = b & 0x03;
                more = 4; // Expect 4 more bytes  
            } else /*if ((b & 0xfe) == 0xfc)*/{ // 1111110x (yields 1 bit)  
                sumb = b & 0x01;
                more = 5; // Expect 5 more bytes  
            }
            /* We don't test if the UTF-8 encoding is well-formed */
        }
        return ();
    }

    public static void main(String[] args){
(URLtoUTF8.toUtf8String("you"));
        (("%E4%BD%A0%20%E5%A5%BD"));

    }
}