-4

I've searched for this on the internet but was unable to find a precise solution, one possible solution I found was to read the integer as a String, and use charAt method and then cast the character to int to print the ascii value.

But is there any other possible way to do it other than the above stated method?

int a=sc.nextInt();
//code to convert it into its equivalent ASCII value.

For example, consider the read integer as 1, and now I want the ASCII value of 1 to be printed on the screen,which is 49

Revanth
  • 87
  • 2
  • 13

2 Answers2

3

I assume you're looking for this:

System.out.print((char)a);
shmosel
  • 49,289
  • 6
  • 73
  • 138
1

The easy way to do that is:


For the Whole String
public class ConvertToAscii{
    public static void main(String args[]){
      String number = "1234";
      int []arr = new int[number.length()];
      System.out.println("THe asscii value of each character is: ");
      for(int i=0;i<arr.length;i++){
          arr[i] = number.charAt(i); // assign the integer value of character i.e ascii
          System.out.print(" "+arr[i]);
      }
    }
}


For the single Character: String number="123"; asciiValue = (int) number.charAt(0)//it coverts the character at 0 position to ascii value
susan097
  • 3,500
  • 1
  • 23
  • 30