Beginners Programs :

Java : Convert Character to ASCII in 2 Ways | Java Programs

Java print and Convert Char to ASCII in Java program in 2 different ways. The converted character to ASCII Java program will be given with sample output and suitable examples.

ASCII is an acronym for American Common Code For Information Interchange.

It is a standard data-transmission code used by smaller and less powerful computers to convey both textual data (letters, numbers, and punctuation marks) and noninput-device commands (control characters).

A character in ASCII is represented by 8 bits. One of the bits, however, is a parity bit. This is used to do a parity check (a form of error checking). Because this consumes one bit, ASCII expresses 128 characters (equal to 7 bits) using 8 bits rather than 256.

The code here is to display from the char to ASCII value of a user-input value using Java language.

  • Everything on a computer is displayed using numbers. This includes all characters as well. The numeric values of these characters are called ASCII values.
  • Every character has a different ASCII value including uppercase and lowercase alphabets, white spaces, exclamation marks and so on.
  • The problem here is to find the ASCII value of a character input by a user or more specifically the value of the first character from a user-input string.
  • The constraint here is the ability to determine the ASCII value of only the first character from the user-defined string.
  • The input here is a string defined by the user.
  • The output here is the ASCII value of the first character of the input string.

Output – 1:

  • Solution :
  1. A new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java.
  2. Scanner class is part of the java.util package and hence the import statement in the beginning is stated to import the functionality of the specified class.
  3. The next line prints a statement to the console instructing the user to enter any character.
  4. The .next() method is called by the Scanner object sc. This method reads a string input word by a user and stores it to the char variable ch.
  5. Note : The .next() method stores the input to a char variable and not a string variable because .charAt(0) method returns the character at index 0 or the first position in the string.
  6. The method characterToAscii is called in the next line. char c is passed as the argument to this method and it returns the ASCII value of the input character and stores it to the int variable n. 
  7. This value of i.e. the ASCII value of the character is printed out to the console by the print statement in the last line. To better understand the output, we shall see what the method characterToAscii does to calculate the ASCII value.
  8. In this method, the parameter char is typecast to an int value (typecasting, or type conversion, is a method of changing an entity from one data type to another).
  9. (int) c – this is how char c is typecast to an int value. Typecasting involves certain rules and principles and it is not feasible any datatype to another without following these rules.
  10. However, in this particular case, when a char value is typecast to an int, the ASCII value of the given char is returned since ASCII is the int representation of a character.
  • Here, the input character is ‘ ! ‘. The ASCII value of an exclamation mark is 33 which is correctly displayed as the output.

Output – 2:

  • Here, the input character is ‘ a ‘. The ASCII value of lowercase alphabet a is 97 which is correctly displayed as the output. Keep in mind that the ASCII values of the same alphabets in different cases are different (example: A = 65 and a = 97).