简体   繁体   English

escaping Java 字符串到 utf-8

[英]escaping Java string to utf-8

I'm looking for java tool for converting regular String to utf-8 string.我正在寻找用于将常规字符串转换为 utf-8 字符串的 java 工具。

eg例如

input: special-数据应用-text input: special-数据应用-text

output: special-%u6570%u636E%u5E94%u7528-text output: special-%u6570%u636E%u5E94%u7528-text

(note the preceding "%u") (注意前面的“%u”)

Two things: 两件事情:

  1. The string you want as result is not UTF-8, at least the string you put as example is sort of UTF-16 encoded (java uses UTF-16 internally) 您要作为结果的字符串不是UTF-8,至少您作为示例输入的字符串是UTF-16编码的(Java在内部使用UTF-16)

  2. An example of code that gives you the string that you want: 为您提供所需字符串的代码示例:

     String str = "special-数据应用-text"; StringBuilder builder = new StringBuilder(); for(char ch: str.toCharArray()) { if(ch >= 0x20 && ch <= 0x7E) { builder.append(ch); } else { builder.append(String.format("%%u%04X", (int)ch)); } } String result = builder.toString(); 

Let me recommend you Unbescape [ http://www.unbescape.org ] 让我向您推荐Unbescape [ http://www.unbescape.org ]

Among other escaping operations (HTML, XML, etc.), it will allow you to escape your Java literal with: 在其他转义操作(HTML,XML等)中,它将允许您使用以下方法转义Java文字:

final String escaped = JavaEscape.escapeJava(text);

Disclaimer, per StackOverflow rules: I'm Unbescape's author. 根据StackOverflow规则免责声明:我是Unbescape的作者。

Can you try the following 你可以尝试以下

StringBuilder b = new StringBuilder();

for( char c : s.toCharArray() ){
    if( ( 1024 <= c && c <= 1279 ) || ( 1280 <= c && c <= 1327) || ( 11744 <= c && c <= 11775) || ( 42560 <= c && c <= 42655)  ){
        b.append( "\\u" ).append( Integer.toHexString(c) );
    }else{
        b.append( c );
    }
}

return b.toString();

Found here 这里找到

尝试这个

String s= URLEncoder.encode(str, "UTF-8").replaceAll("%(..)%(..)", "%u$1$2");

For those who don't need java tool, but needs the online tool here is the tool https://itpro.cz/juniconv/对于那些不需要 java 工具,但需要在线工具的人,这里是工具https://itpro.cz/juniconv/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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