\uxxxx - Entering Unicode Data in Java Programs

This section provides a tutorial example on how to enter Unicode characters using \uxxxx escape sequences in a Java program, and same them to any giving character set encoding.

Encoding conversion is about reading strings of characters stored in a file encoded with encoding A, and writing them into another file encoded with encoding B.

Before going into details on encoding conversion, let's talk briefly about Unicode data entry. How do we enter Unicode characters into a file? There are a couple of ways to do that:

Word processors are too specific to be discussed here.

Hex editors are ultimate data entry tools for Unicode characters. They can also be used to inspect and repair encoded text files. But Hex editors are very hard to use. Note that Notepad on Windows is not a Hex editor. But UltraEdit on Windows is a Hex editor.

Using Unicode based programming language, like Java, to enter Unicode characters into a file is very interesting. For each character in a string literal, you can use the \uxxxx escape sequence to represent the character by enter its code point value in Hex format.

Here is a sample program, UnicodeHello.java, showing you how to use \uxxxx escape sequences:

/* UnicodeHello.java
 * Copyright (c) 2019 HerongYang.com. All Rights Reserved.
 *
 * This program is a simple tool to allow you to enter several lines of
 * text, and write them into a file using the specified encoding 
 * (charset name). The input text lines uses Java string convention, 
 * which allows you to enter ASCII characters directly, and any non
 * ASCII characters with escape sequences.
 *
 * This version of the program is to write out the "Hello world!" 
 * message in some different languages.
 */
import java.io.*;
class UnicodeHello {
   public static void main(String[] a) {
      // The following Array contains text to be saved into the output
      // File. To enter your own text, just replace this Array.
      String[] text = {
"Hello computer! - English", // ASCII
"\u7535\u8111\u4F60\u597D\uFF01 - Simplified Chinese", // GB2312
"\u96FB\u8166\u4F60\u597D\uFE57 - Traditional Chinese" // Big5
      };
      String outFile = "hello.utf-16be";
      if (a.length>0) outFile = a[0];
      String outCharsetName = "utf-16be";
      if (a.length>1) outCharsetName = a[1];
      String crlf = System.getProperty("line.separator");
      try {
         OutputStreamWriter out = new OutputStreamWriter(
            new FileOutputStream(outFile), outCharsetName);
         for (int i=0; i<text.length; i++) {
            out.write(text[i]);
            out.write(crlf);
         }
         out.close();
      } catch (IOException e) {
         System.out.println(e.toString());
      }
   }
}

As you can see from the source code, this program will write the "Hello computer!" message in several languages. Let's compile this program and run it to get the characters saved into a file with UTF-16BE encoding:

C:\herong>javac UnicodeHello.java

C:\herong>java UnicodeHello hello.utf-16be utf-16be

Now we have a text file with characters saved in UTF-16BE encoding. Read the next section on how to view and inspect this UTF-16BE encoded file.

Table of Contents

 About This Book

 Character Sets and Encodings

 ASCII Character Set and Encoding

 GB2312 Character Set and Encoding

 GB18030 Character Set and Encoding

 JIS X0208 Character Set and Encodings

 Unicode Character Set

 UTF-8 (Unicode Transformation Format - 8-Bit)

 UTF-16, UTF-16BE and UTF-16LE Encodings

 UTF-32, UTF-32BE and UTF-32LE Encodings

 Python Language and Unicode Characters

 Java Language and Unicode Characters

 Character Encoding in Java

 Character Set Encoding Maps

Encoding Conversion Programs for Encoded Text Files

\uxxxx - Entering Unicode Data in Java Programs

 HexWriter.java - Converting Encoded Byte Sequences to Hex Values

 EncodingConverter.java - Encoding Conversion Sample Program

 Viewing Encoded Text Files in Web Browsers

 Unicode Signs in Different Encodings

 Using Notepad as a Unicode Text Editor

 Using Microsoft Word as a Unicode Text Editor

 Using Microsoft Excel as a Unicode Text Editor

 Unicode Fonts

 Archived Tutorials

 References

 Full Version in PDF/EPUB