ASCII in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

ASCII (American Standard Code for Information Interchange) is a character encoding system used to represent text in computers and other devices. It assigns a unique numerical value to each character, including letters, numbers, punctuation marks, and other symbols. In the ASCII system, each character is represented by a 7-bit binary code.

It can only represent a maximum of 128 characters because it is a seven-bit code**. There are 95 printable characters now defined by it, including 26 upper cases (A to Z), 26 lower cases, 10 numbers (0 to 9), and 33 special characters such as mathematical symbols, punctuation marks, and space characters.

In Java, ASCII is used as the default character encoding for text data.

How to Print ASCII in Java?

ASCII (American Standard Code for Information Interchange) is a system that assigns unique numerical values to characters so that computers can store and manipulate them. The assigned numerical values are represented in binary form for use by electrical equipment, since it is not possible to use or store the original character form.

There are four methods available in Java to print the ASCII value. Each method is briefly explained below, along with an example of how to implement it:

  • Brute force technique
  • Type-casting method
  • Format specifier method
  • Byte class method [Most Optimal]

Method 1 - Using Brute Force Method

Simply assign the character to a new integer-type variable to determine the character's ASCII value. Java automatically inserts the character's ASCII value into the new int variable.

We will determine the ASCII value of the character entered by the user in this example.

Code:

Output

Code Explanation

  • We have created an object of the Scanner class to get input from the user.
  • We then stored the character entered by the user in a char type variable and assigned the char value to an int type variable.
  • Upon assigning the ascii value of the character gets stored in the int variable.

Method 2 - Using the Type-casting Method

In Java, type-casting is a technique for changing a variable's datatype. To use type casting to get the ASCII value of a character in Java, you can follow these steps:

  • Declare a character variable and assign a value to it.
  • Type cast the character variable to an integer using (int).
  • Print the ASCII value using System.out.println().

Code:

Output

Method 3 - Using the format() Method

The format() method in Java is used to format a string using a specified format string and arguments.

The sprintf() function in the c programming language and the format() method in python achieve similar functionality.

In this method, a format specifier is used to produce the ASCII value of the given character. By designating the character to be an int, we were able to save the value of the supplied character inside a format specifier. As a result, the format specifier contains the character's ASCII value.

In this example, we are going to learn how to get the ASCII value of a character using a format specifier.

Output

Code Explanation

  • In the above code, we have used a format specifier to get the ASCII value of a character.
  • First, we have created an object of Formatter and converted the character to an integer using the %d format specifier.

Method 4 - Using getBytes() Method

This is the most optimal way to get the ASCII value of a character

  • Initialize the character as a string.
  • Create an array of type bytes using getBytes() method.
  • Print the element at the 0th index of the byte array.

This technique is typically used to translate an entire string to its ASCII values. The try-catch is provided for characters that violate the encoding exception.

In this example, we are going to get the ASCII value of a character using the Byte class method which happens to be the most optimal technique.

Output

Code Explanation

  • In the above code, we have created a byte array from a single character string using the ASCII encoding and prints the ASCII value of that character.
  • In the above Java code, the getBytes() method is used to encode a string into a sequence of bytes using a specific character set.
  • In this case, the getBytes() method takes the "US-ASCII" character set as an argument, which means that the string will be encoded using the US-ASCII encoding standard.
  • If an exception occurs due to unsupported encoding, a message is printed to indicate the issue.

ASCII Table

In total, there are 256 ASCII characters, and can be broadly divided into three categories:

  • ASCII control characters (0-31 and 127)
  • ASCII printable characters (32-126) (most commonly referred)
  • Extended ASCII characters (128-255)
CharacterCharacter NameASCII code CharacterCharacter NameASCII code CharacterCharacter NameASCII code
!Exclamation point33 AUppercase a65 aLowercase a97
Double quotation34 BUppercase b66 bLowercase b98
#Number sign35 CUppercase c67 cLowercase c99
$Dollar sign36 DUppercase d68 dLowercase d100
%Percent sign37 EUppercase e69 eLowercase e101
&ampersand38 FUppercase f70 fLowercase f102
apostrophe39 GUppercase g71 gLowercase g103
(Left parenthesis40 HUppercase h72 hLowercase h104
)Right parenthesis41 IUppercase i73 iLowercase i105
*asterisk42 JUppercase j74 jLowercase j106
+Plus sign43 KUppercase k75 kLowercase k107
,comma44 LUppercase l76 lLowercase l108
hyphen45 MUppercase m77 mLowercase m109
.period46 NUppercase n78 nLowercase n110
/slash47 OUppercase o79 oLowercase o111
0zero48 PUppercase p80 pLowercase p112
1one49 QUppercase q81 qLowercase q113
2two50 RUppercase r82 rLowercase r114
3three51 Suppercases83 sLowercase s115
4four52 TUppercase t84 tLowercase t116
5five53 UUppercase u85 uLowercase u117
6six54 VUppercase v86 vLowercase v118
7seven55 WUppercase w87 wLowercase w119
8eight56 XUppercase x88 xLowercase x120
9nine57 YUppercase y89 yLowercase y121
:colon58 ZUppercase z90 zLowercase z122
;semi-colon59 [Left square bracket91 {Left curly brace123
<Less-than sign60 \backslash92 |Vertical bar124
=Equals sign61 ]Right square bracket93 }Right curly brace125
>Greater-than sign62 ^caret94 ~tilde126
?Question mark63 _underscore95    
@At sign64 `Grave accent96    


Conclusion

  • ASCII stands for American Standard Code for Information Interchange and it's a character encoding system which assigns a unique numerical value to each character, including letters, numbers, punctuation marks, and other symbols.
  • Different letters and symbols are assigned a distinct numerical value in ASCII so that computers can store and manipulate them. The hardware always operates with the binary value of the assigned ASCII number. Since doing so in the original form is impossible.
  • There are four different ways to output an ASCII value in java for a particular character.
    1. Using the brute force Method
    2. Using the type-casting Method
    3. Using the format specifier Method
    4. Using Byte class Method
  • We can use the format() method in Java to format a string using a specified format string and arguments. The sprintf() function in the c programming language and the format() method in Python achieve similar functionality
  • The Byte class method is the most optimal method. We use the Byte class's static method to get the ASCII value of a character. This method is more efficient than the other three methods.