Escape Unicode Characters In Java Strings

I was looking for a code snippet that does that for me but couldn’t find one, especially since native2ascii seems to skip a few characters I need to show. If you’re in a similar situation, feel free to use this:

package at.soher.unicode;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.FieldPosition;

public class StringEscape {

    public static void main(String[] args) {

        File file = new File("~/file.txt");
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(file), "UTF-16"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] fields = line.split("[\t]");
                if (fields.length > 1) {
                    StringBuilder newLine = new StringBuilder();
                    newLine.append(fields[0] + " = ");

                    String input = fields[2];
                    StringBuilder dest = new StringBuilder();
                    for (int i = 0; i < input.length(); i++) {
                        if (input.charAt(i) > 127) {
                            dest.append("\\u" + asHex((long) input.charAt(i)));
                        } else {
                            dest.append(input.charAt(i));
                        }
                    }
                    newLine.append(dest);
                    System.out.println(newLine.toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String asHex(long number) {
        String hexString = "0000" + Long.toHexString(number).toUpperCase();
        return hexString.substring(hexString.length() - 4);

    }

}

Prague

At the beginning of the month, I spent a few days in Prague. Here are a few impressions.


As usual, all images are available under a Creative Commons License.

March and Rally for Human Rights at AIDS 2010

Last week, the XVIII International AIDS Conference (AIDS 2010) took place in Vienna. Part of it was a rally for human rights on the 20th of July, which was headed by singer Annie Lennox, who also performed. I was there and took a few pictures. Please also take some time and read about the current struggle against HIV/AIDS and the 10 reasons why human rights should occupy the center of the global AIDS struggle.