Library to convert native2ascii and vice versa

I am looking for a library (Apache / BSD / EPL license) for converting native text to ASCII using \ u for characters not available in ASCII (basically what java.util.Properties is).

I looked and there seems to be no libraries available. I found:

Does anyone know the library with the above licenses?

+6
source share
2 answers

You can do this with CharsetEncoder. You should read the "native" text with the correct encoding in unicode. How can you use the "US-ASCII" encoder to detect which characters should be translated in unicode escape sequences.

import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; import org.junit.Test; public class EncodeToEscapes { @Test public void testEncoding() { final String src = "Hallo äöü"; // this has to be read with the right encoding final CharsetEncoder asciiEncoder = Charset.forName("US-ASCII").newEncoder(); final StringBuilder result = new StringBuilder(); for (final Character character : src.toCharArray()) { if (asciiEncoder.canEncode(character)) { result.append(character); } else { result.append("\\u"); result.append(Integer.toHexString(0x10000 | character).substring(1).toUpperCase()); } } System.out.println(result); } } 

In addition, org.apache.commons: commons-lang contains StringEscapeUtils.escapeJava (), which can hide and undo native lines.

+10
source

Try this piece of code from Apache commons-lang:

 StringEscapeUtils.escapeJava("ایران زیبای من"); StringEscapeUtils.unescapeJava("\u0627\u06CC\u0631\u0627\u0646 \u0632\u06CC\u0628\u0627\u06CC \u0645\u0646"); 
+3
source

Source: https://habr.com/ru/post/912371/


All Articles