Java Program For Int to Char Conversion (original) (raw)
Last Updated : 09 Apr, 2025
In this article, we will check **how to convert an Int to a Char in Java. In Java, char takes 2 bytes (16-bit UTF encoding ), while int takes 4 bytes (32-bit). So, if we want the integer to get converted to a character then we need to typecast because data residing in 4 bytes cannot get into a single byte. Every character has its own unique code called a **Unicode value, which is represented by a numeric value. Here, we convert the numerical value to its equivalent ASCII representation.
Here, the input provided to us is an integer value, say it is ‘**N’, and the task is to convert the number into characters.
**Example of Int to Char Conversion:
**Input : N = 97
**Output : a ( Smaller case )**Input : N = 98
**Output : b ( Smaller case )
**Methods for Int to Char Conversion in Java
- Using the concept of typecasting (Naive Approach)
- Using Character.forDigit() function
Let us discuss each of the above-listed methods by laying out their corresponding approach and implementing them with the help of clean Java code.
1. Using the concept of Type-casting
Here we will simply explicitly typecast the datatype and store it into a character. As we all know, there is an ASCII table that holds a specific value corresponding to 256 symbols of the English language. So automatically it will store the corresponding symbol corresponding to the digit.
**Example 1:
Java `
// Java Program to Illustrate Integer to // Character Conversion // Using Concept of Type-casting
// Importing required classes import java.util.*;
// Driver Class class Geeks {
// Main method
public static void main(String[] args)
{
// Custom integer input
int i = 97;
// Type casting character to integer
char ch = (char)i;
// Print statement
System.out.println(ch);
}
}
`
Similarly, we can do the same via adding zero to the integer value and later on we will do the same as we did in the above example that is we will typecast the above same.
**Example 2:
Java `
// Java Program to Convert Integer to Character // of Integer Class Via Type-casting With Adding Zero import java.io.; import java.lang.; import java.util.*;
class Geeks { public static void main(String[] args) {
int i = 64;
char ch = (char)(i + '0');
System.out.println(ch);
}
}
`
**Tip: It is also recommended to go through difference between type conversion and type casting in java.
2. Using Character.forDigit() for Converting Int to Char
This method using for converting integers to char, where we convert based on the base value of the Number. Converting is all based upon the base value it can’t be misunderstood by ASCII value.
Example 1:
Java `
// Java Program to Convert Int // to Char using Character.forDigit() import java.io.*;
// Driver Class class GFG { // main function public static void main(String[] args) { int base = 10; int a = 5;
char ans = Character.forDigit(a, base);
System.out.println(ans);
}
}
`
Example 2:
Java `
// Java Program to Convert Int // to Char using Character.forDigit() import java.io.*;
// Driver Class class Geeks { // main function public static void main(String[] args) { int base = 16; int a = 1;
while (a < 16) {
char ans = Character.forDigit(a, base);
System.out.print(ans+" ");
a++;
}
}
}
`
Output
1 2 3 4 5 6 7 8 9 a b c d e f