Java Program for Character to ASCII Conversion

Character to Ascii conversion

ASCII is the abbreviation of American Standard Code for Information Interchange. It is a character encoding standard for electronic communication. ASCII encodes 128 specified characters into seven-bit integers. It contains the digits from 0 - 9, the upper and lower case English letters from A to Z and some special characters.


Example:

ASCII value of lowercase 'c' is 99.

ASCII value of digit '0' is 48.


  The following Java program converts a given character to its corresponding ASCII value. The algorithm, pseudocode and time complexity of the program have also been added.






1. Algorithm for Character to ASCII Conversion


1. Take a character as input.

2. Do cast the character to integer

3. Return the result




2. Pseudocode for Character to ASCII Conversion


Input : A character $c$

Output : ASCII value of character $c$

1. Procedure toAscii($c$):

2. Return (Int($c$))

3. End Procedure





3. Time complexity for Character to ASCII Conversion


Time Complexity: O(1)





4. Java Program & output for Character to ASCII Conversion

Code has been copied
/*********************************************
            Alphabetacoder.com
Java proram for character to ASCII conversion
**********************************************/

import java.util.Scanner;
class ASCII {
    public static void main(String args[]) {
        int v;
        char x;
        //System.in is a standard input stream
        // sc is the object
        Scanner sc = new Scanner(System.in);
        //take input of a character
        System.out.print("Enter the character = ");
        x = sc.next().charAt(0);
        //store ASCII value of the character
        v = (int) x;

        System.out.print("\nASCII value of " + x + " is = " + v);
    }
}

Output


Case 1:

Enter the character = c

ASCII value of c is = 99


Case 2:

Enter the character = C

ASCII value of C is = 67