开发者

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

开发者 https://www.devze.com 2023-02-19 09:27 出处:网络
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 sequenc

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 开发者_StackOverflow社区arrays?


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


This worked for me. Thanks for all the feedback!

final Charset charset = Charset.forName(charsetName);
for (int i = 0; i < 255; i++) {
    ByteBuffer bb = ByteBuffer.allocate(4);
    bb.putInt(i);
    System.out.println(new String(bb.array(), charset).trim());
}


Iterate over the values from 0 to 127 (or 255) and decode them using the wanted character set, resulting in “normal” java char values. Now you can check for the alphanumericality of those values using Character.isLetterOrDigit(char) and use them to your liking.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号