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

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

+4
source share
3 answers

It worked for me. Thanks for all the reviews!

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()); } 
+4
source

Try the following to print all valid characters in order of 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 creates something that matches http://www.fileformat.info/info/charset/IBM037/grid.htm

+3
source

Iterate the values ​​from 0 to 127 (or 255 ) and decode them using the desired character set, resulting in "normal" java char values. Now you can check the alphanumeric value of these values ​​with Character.isLetterOrDigit (char) and use them as you wish.

0
source

Source: https://habr.com/ru/post/1344747/


All Articles