Languages
[Edit]
EN

Java - convert int to char

12 points
Created by:
Emelia
327

Short solution:

int number = 65;
char letter = (char) number;

System.out.println(number); // 65
System.out.println(letter); // A

Other simple solution:

int number = 'A';
char letter = (char) number;

System.out.println(number); // 65
System.out.println(letter); // A

Below we have full examples of this short solutions and int to ASCII char casting examples.

1. Using explicit char casting

public class Example1 {

    public static void main(String[] args) {

        int number = 65;
        char letter = (char) number;

        System.out.println(number); // 65
        System.out.println(letter); // A
    }
}

Output:

65
A

2. Using explicit char casting next example

public class Example2 {

    public static void main(String[] args) {

        int number = 'A';
        char letter = (char) number;

        System.out.println(number); // 65
        System.out.println(letter); // A
    }
}

Output:

65
A

3. Using char casting to print ASCII digits and letters

public class Example3 {

    public static void main(String[] args) {

        for (int i = 48; i < 48 + 10; i++) {
            char letter = (char) i;
            System.out.print(letter + ", ");
        }
        System.out.println();

        for (int i = 65; i < 65 + 26; i++) {
            char letter = (char) i;
            System.out.print(letter + ", ");
        }
        System.out.println();

        for (int i = 97; i < 97 + 26; i++) {
            char letter = (char) i;
            System.out.print(letter + ", ");
        }
        System.out.println();
    }
}

Output:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
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,
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,

4. Print all int to ASCII char casting - digit values 0-9

public class Example4 {

    public static void main(String[] args) {

        for (int i = 48; i < 48 + 10; i++) {
            char letter = (char) i;
            System.out.println("int: " + i + " -> char: " + letter);
        }
    }
}

Output:

int: 48 -> char: 0
int: 49 -> char: 1
int: 50 -> char: 2
int: 51 -> char: 3
int: 52 -> char: 4
int: 53 -> char: 5
int: 54 -> char: 6
int: 55 -> char: 7
int: 56 -> char: 8
int: 57 -> char: 9

5. Print all int to ASCII char casting - uppercase letter values A-Z

public class Example5 {

    public static void main(String[] args) {

        for (int i = 65; i < 65 + 26; i++) {
            char letter = (char) i;
            System.out.println("int: " + i + " -> char: " + letter);
        }
    }
}

Output:

int: 65 -> char: A
int: 66 -> char: B
int: 67 -> char: C
int: 68 -> char: D
int: 69 -> char: E
int: 70 -> char: F
int: 71 -> char: G
int: 72 -> char: H
int: 73 -> char: I
int: 74 -> char: J
int: 75 -> char: K
int: 76 -> char: L
int: 77 -> char: M
int: 78 -> char: N
int: 79 -> char: O
int: 80 -> char: P
int: 81 -> char: Q
int: 82 -> char: R
int: 83 -> char: S
int: 84 -> char: T
int: 85 -> char: U
int: 86 -> char: V
int: 87 -> char: W
int: 88 -> char: X
int: 89 -> char: Y
int: 90 -> char: Z

6. Print all int to ASCII char casting - lowercase letter values a-z

public class Example6 {

    public static void main(String[] args) {

        for (int i = 97; i < 97 + 26; i++) {
            char letter = (char) i;
            System.out.println("int: " + i + " -> char: " + letter);
        }
    }
}

Output:

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

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join