Question

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

e.g.

input: special-数据应用-text

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

(note the preceding "%u")

Was it helpful?

Solution 2

Try this

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

OTHER TIPS

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

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

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.

For those who don't need java tool, but needs the online tool here is the tool https://itpro.cz/juniconv/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top