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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With