PowerFlower
PowerFlower

Reputation: 1619

Print all UTF-16 Letters to console (Java)

I have actually a very simple code. I just try to print out all UTF-16 signs. It works particular but most of the signs this program prints are not readable.

  public static void main(String[] args) {
    for (int i = 0; i < 65535; i++) {
        try {
            System.out.println(new String(ByteBuffer.allocate(4).putInt(i).array(), "UTF-16"));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Charset.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

Why is that and how can i please fix it?

Thank you

Upvotes: 1

Views: 329

Answers (2)

Raedwald
Raedwald

Reputation: 48692

Not all 16 bit integers are valid Unicode codepoints. Also, for the valid codepoints, your display device must have a full set of fonts, which is rarely the case.

Upvotes: 1

Vlad
Vlad

Reputation: 18633

You'd probably want a short rather than an int. I'd say you want to allocate 2 bytes rather than 4, and use putShort().

That said, UTF-16 is actually variable-length, so it won't be as simple as just printing every code point. Check the article for details. It also depends on whether whatever font you're using has the right glyphs.

Upvotes: 0

Related Questions