将unicode字符串转换为带有'%XX'字符的转义ASCII字符串

时间:2017-12-12 15:30:01

标签: java unicode ascii

我的java代码可以获取带有UTF字符的网址,这些网址应转换为“转义”字符,例如rootPath = "C:\\Users\\user" pattern = re.compile(r"^.+[^\d](\d+)$") latestFolderNumber = 0 dirs = [entry for entry in os.listdir(rootPath) if os.path.isdir(os.path.join(rootPath, entry))] for name in dirs: match = pattern.match(name) if match: latestFolderNumber = int(match.group(1)) print("The latest number is " + latestFolderNumber) 等。

有没有办法进行转换?

2 个答案:

答案 0 :(得分:2)

看看java.net.URLEncoder。或者您可以基于String.format创建自己的编码器:

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

将c转换为%CB%00

隐藏字符串:

    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);

结果:

1%CB%002

答案 1 :(得分:2)

使用此:URLEncoder.encode(str2," utf-8")