简体   繁体   中英

escaping Java string to utf-8

I'm looking for java tool for converting regular String to utf-8 string.

eg

input: special-数据应用-text

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

(note the preceding "%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)

  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 ]

Among other escaping operations (HTML, XML, etc.), it will allow you to escape your Java literal with:

final String escaped = JavaEscape.escapeJava(text);

Disclaimer, per StackOverflow rules: I'm Unbescape's author.

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/

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