"native2ascii -encoding" - UTF-8 to uXXXX Conversion

This section provides a tutorial example on how to convert UTF-8 character strings to \uXXXX Unicode code sequences with the 'native2ascii -encoding' command.



Now let's see how we can fix the encoding problem with HelloUtf8.java demonstrated in the previous section.

1. Convert HelloUtf8.java to \uXXXX Unicode code sequences using the "native2ascii -encoding utf-8" command:

C:\herong>\Progra~1\java\jdk1.8.0\bin\native2ascii -encoding utf-8 
   HelloUtf8.java HelloUtf8Converted.java

2. Rename the class name in HelloUtf8Converted.java with an editor:

/* HelloUtf8Converted.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
public class HelloUtf8Converted {
   public static void main(String[] a) {
      System.out.println("Hello world!"); 	
      System.out.println("\u4e16\u754c\u4f60\u597d\uff01"); 	
   }
}

3. Compile and run HelloUtf8Converted.java:

C:\herong>javac HelloUtf8Converted.java

C:\herong>java HelloUtf8Converted

Hello world!
?????

What happens to the Chinese string printed on the console? Why I am not getting Chinese characters back in the output?

The problem is not caused by those \uXXXX Unicode code sequences used to represent the Chinese string. Those \uXXXX Unicode code sequences correctly inserted Chinese characters into the storage of a string variable. The problem is caused by the default encoding used by the "out" stream. See the next section on how to fix this problem.



 

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 java - The Java Program Launcher

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jhsdb - The Java HotSpot Debugger

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

native2ascii - Native-to-ASCII Encoding Converter

 native2ascii - Encoding Converter Command and Options

 javac - Using CP1252 to Process Source File

"native2ascii -encoding" - UTF-8 to uXXXX Conversion

 Setting UTF-8 Encoding in PrintStream

 Converting uXXXX Sequences Back with "-reverse" Option

 Outdated Tutorials

 References

 Full Version in PDF/EPUB