Open In App
Related Articles

Java Program For Int to Char Conversion

Improve Article
Improve
Save Article
Save
Like Article
Like

As we are aware char in Java takes 1 byte while int takes 4 bytes. So if we want the integer to get converted to a character then we need to typecast because data residing in 4 bytes can not get into a single byte. So prior to moving ahead, readers must have to be well-versed in typecasting in Java.

Tip: It is also recommended to go through difference between type conversion and type casting in java. 

There are many operations that can be computed over strings but not over characters so we generally see in Java that many operations are computed. Here the input provided to us is an integer value say it be ‘N’ and the task is to convert the number in characters.

Example of Int to Char Conversion

Input : N = 74254 
Output : Seven four two five four

Input : N = 23 
Output : Two three

Methods for Int to Char Conversion in Java

  1. Using the concept of typecasting (Naive Approach)
  2. 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 codes.

1. Using the concept of Type-casting

Here we will simply explicitly typecast the datatype and will store it into 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:

Java




// Java Program to Illustrate Integer to
// Character Conversion
// Using Concept of Type-casting
  
// Importing required classes
import java.util.*;
  
// Class
class GFG {
  
    // Main driver 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);
    }
}


Output

a


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:

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 GFG {
    public static void main(String[] args)
    {
  
        int i = 64;
        char ch = (char)(i + '0');
        System.out.println(ch);
    }
}


Output

p


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);
    }
}


Output

5


Example 2:

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 = 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 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 20 Aug, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials