HexWriter.java - Converting Encoded Byte Sequences to Hex Values

This section provides a tutorial example on how to write a sample program, HexWriter.java, to convert encoded byte sequences to Hex values to help viewing encoded text files.

By running the sample program, UnicodeHello.java, presented in the previous section, I got this text file saved in UTF-16BE encoding, hello.utf-16be. The next question is how can I view and inspect this UTF-16BE encoded file. Normal text editors will not able to show the content of this correctly.

I have two choices: using a Hex editor to open the file or convert the file to Hex value file with a program.

I decide to write a simple Java program convert UTF-16BE byte sequences into Hex decimal digits to allow me inspecting the code values of the saved characters. Remember UTF-16BE encoding breaks the code values into two bytes directly without any changes in the value. Here is a program to convert any data file into Hex decimal digits:

/* HexWriter.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 * This program allows you to convert and data file to a new data
 * in Hex format with 16 bytes (32 Hex digits) per line.
import java.io.*;
class HexWriter {
   static char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                             '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
   public static void main(String[] a) {
      String inFile = a[0];
      String outFile = a[1];
      int bufSize = 16;
      byte[] buffer = new byte[bufSize];
      String crlf = System.getProperty("line.separator");
      try {
         FileInputStream in = new FileInputStream(inFile);
         OutputStreamWriter out = new OutputStreamWriter(
            new FileOutputStream(outFile));
         int n = in.read(buffer,0,bufSize);
   String s = null;
         int count = 0;
         while (n!=-1) {
            count += n;
            s = bytesToHex(buffer,0,n);
            n = in.read(buffer,0,bufSize);
         System.out.println("Number of input bytes: "+count);
      } catch (IOException e) {
   public static String bytesToHex(byte[] b, int off, int len) {
      StringBuffer buf = new StringBuffer();
      for (int j=0; j<len; j++)
      return buf.toString();
   public static String byteToHex(byte b) {
      char[] a = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
      return new String(a);

Compile this program and run it to convert hello.utf-16be:

herong> javac HexWriter.java
herong> java HexWriter hello.utf-16be hello.hex

Okay, here is the content of hello.hex:


If you know how to read Hex number, you should be able to see:

Remember to use line break sequence 000D000A (\r\n) to help finding the first character of each line.

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 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

 Java Logging

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 Full Version in PDF/EPUB