简体   繁体   中英

Convert unicode string to an escaped ASCII string with '%XX' characters

I have java code that gets URLs with UTF characters, that should be converted to "escaped" characters, such as %A4%FD etc.

Is there a way to make this conversion?

Take a look at java.net.URLEncoder. Or you can make your own encoder based on String.format:

char c = 'Ë';
String hex = System.out.printf("%%%02X%%%02X", c & 0xFF, (c >> 8) & 0xFF);

this converts c to %CB%00

To covert a string:

    String s = "1Ë2";
    StringBuilder sb = new StringBuilder();
    for(char c : s.toCharArray()) {
        if (c > 128) {
            sb.append(String.format("%%%02X%%%02X", c & 0xFF, (c >> 8) & 0xFF));
        } else if (c == '%'){
            sb.append("%%");
        } else {
            sb.append(c);
        }
    }
    System.out.println(sb);

result:

1%CB%002

只需使用此:URLEncoder.encode(str2,“ utf-8”)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM