Java program to print the ASCII value of an integer

Published by admin on

Java print ASCII value of an integer

Java program to print ASCII value of an integer :

American Standard Code for Information Interchange or ASCII code links an integer value to a symbol like letter, digit , special character etc. In ASCII 7 bits are used to represent a character. From 0 to 127, each number can be represent by a character. In this tutorial, we will learn how to print ASCII value of an integer.

Java Program :

import java.util.Scanner;

public class Main {

    /**
     * Utility functions
     */
    static void println(String string) {
        System.out.println(string);
    }

    static void print(String string) {
        System.out.print(string);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        println("********** Java program to convert ASCII to Character **********");
        println("");

        int ascii;

        println("Enter a ASCII value within 0 to 127 :");

        //read the integer value
        ascii = sc.nextInt();

        if (ascii > 127) {
            println("Please enter a valid input !!");
        } else {
            //convert the integer value to character
            char asciiChar = (char) ascii;

            println("ASCII value of " + ascii + " is " + asciiChar);
        }
    }
}

Sample Output:

Enter a ASCII value within 0 to 127 :
124
ASCII value of 124 is |

Enter a ASCII value within 0 to 127 :
82
ASCII value of 82 is R

Enter a ASCII value within 0 to 127 :
92
ASCII value of 92 is \

Enter a ASCII value within 0 to 127 :
120
ASCII value of 120 is x
Categories: java

2 Comments

MALATHY · June 7, 2019 at 7:47 am

If i wan tto give the continous 4 ascii value as input.
what is the change in above code?

    admin · June 8, 2019 at 2:53 am

    Hi Malathy,
    You can use one loop for that.
    Take the starting ascii value and use one loop to print all four. For example, if the user enters “45”, using one ‘for loop’, you can print the values for 45,46,47 and 48.

Leave a Reply

Your email address will not be published. Required fields are marked *