How to convert a range of ASCII codes to characters in Java

3 Answers

0 votes
class Main {
    public static void main(String[] args) {
        for (int i = 97; i < 123; i++) {
            char character = (char)i;
            System.out.println(i + ": " + character);
        }
    }
}



/*
run

97: a
98: b
99: c
100: d
101: e
102: f
103: g
104: h
105: i
106: j
107: k
108: l
109: m
110: n
111: o
112: p
113: q
114: r
115: s
116: t
117: u
118: v
119: w
120: x
121: y
122: z

*/

 



answered Nov 6, 2023 by avibootz
edited Sep 8 by avibootz
0 votes
import java.util.List;
import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        List<Character> chars = new ArrayList<>();

        for (int code = 65; code <= 90; code++) {  // A–Z
            chars.add((char) code);
        }
        
        System.out.println(chars);
    }
}


/*
run

[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]

*/

 



answered Sep 8 by avibootz
0 votes
import java.util.List;

class Main {
    public static void main(String[] args) {
       List<Character> chars =
            java.util.stream.IntStream.rangeClosed(65, 90)
            .mapToObj(i -> (char) i)
            .toList();

        
        System.out.println(chars);
    }
}


/*
run

[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]

*/

 



answered Sep 8 by avibootz

Related questions

1 answer 273 views
1 answer 272 views
1 answer 381 views
1 answer 318 views
1 answer 203 views
...