Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate/navigate through each character in a character set (e.g., US-ASCII or IBM037, in proper sequence)?

Tags:

java

I want to iterate through each character in a character set (primarily US-ASCII and IBM037) and then print all alphanumeric characters (or all printable characters) in the proper character set sequence. Is it possible without creating static arrays?

like image 865
Gyles Avatar asked Mar 22 '11 09:03

Gyles


People also ask

What type of a structure is the best way to iterate through the characters of a string?

Using the character iterator is probably the only correct way to iterate over characters, because Unicode requires more space than a Java char provides. A Java char contains 16 bit and can hold Unicode characters up U+FFFF but Unicode specifies characters up to U+10FFFF.

How does the ASCII character set work?

The ASCII character set is a 7-bit set of codes that allows 128 different characters. That is enough for every upper-case letter, lower-case letter, digit and punctuation mark on most keyboards. ASCII is only used for the English language.

How do you print a character using ASCII value in C ++ in loop?

We can use cast type here, by casting into char we are able to get result in character format. We can use cout<<char(65) or cout<<char(var), that will print 'A'. (65 is the ASCII value of 'A'). Here, output will be A.


1 Answers

Try the following to print all the valid characters in the order of the encoded values.

public static void main(String... args) {
    printCharactersFor("US-ASCII");
    printCharactersFor("IBM037");
}

private static void printCharactersFor(String charsetName) {
    System.out.println("Character set map for " + charsetName);
    Charset charset = Charset.forName(charsetName);
    SortedMap<BigInteger, String> charsInEncodedOrder = new TreeMap<BigInteger, String>();
    for (int i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
        String s = Character.toString((char) i);
        byte[] encoded = s.getBytes(charset);
        String decoded = new String(encoded, charset);
        if (s.equals(decoded))
            charsInEncodedOrder.put(new BigInteger(1, encoded), i + " " + s);
    }
    for (Map.Entry<BigInteger, String> entry : charsInEncodedOrder.entrySet()) {
        System.out.println(entry.getKey().toString(16) + " " + entry.getValue());
    }
}

and it produces something which matches http://www.fileformat.info/info/charset/IBM037/grid.htm

like image 50
Peter Lawrey Avatar answered Oct 21 '22 03:10

Peter Lawrey