Java Program to Print the ASCII Value
Last Updated :
24 May, 2024
ASCII is an acronym that stands for American Standard Code for Information Interchange. In this, a specific numerical value is assigned to different characters and symbols, it is a 7-bit character set containing 128 (0-127), for computers to store and manipulate, and while storing and manipulating the electronic device always works with the binary value of the ASCII number given. As it is impossible to do that in the original form. This article focuses on discussing ways to print the ASCII Value.
1. Brute-force Method
To find the ASCII value of a character, simply assign the character to a new variable of integer type. Java automatically stores the ASCII value of that character inside the new variable.
Below is the Java program to implement the approach:
Java
// Java program to print ASCII Value of Character
// by assigning variable to integer
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Character whose ASCII is to be computed
char ch = '}';
// Creating a new variable of type int
// and assigning the character value.
int ascii = ch;
/* Java stores the ascii value there itself*/
// Printing the ASCII value of above character
System.out.println("The ASCII value of " + ch
+ " is: " + ascii);
}
}
OutputThe ASCII value of } is: 125
2. Using Type-Casting
Type-casting in java is a way to cast a variable into another datatype which means holding a value of another datatype occupying lesser bytes. In this approach, a character is a typecast of type char to the type int while printing, and it will print the ASCII value of the character.
Below is the Java program to implement the approach:
Java
// Java program to print ASCII Value of Character
// using type-casting
// Importing java generic libraries
import java.util.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Character whose ASCII is to be computed
char ch = '}';
// Typecasting the character to int and
// printing the same
System.out.println("The ASCII value of " + ch
+ " is: " + (int)ch);
}
}
OutputThe ASCII value of } is: 125
Note:
In above method 1 and method 2, both the methods are one type of typecasting. In method 1, typecasting is done automatically by the compiler. In method 2, typecasting it manually so the method 2 is much more efficient than method 1 as the compiler has to put lesser effort. Also, remember typecasting done automatically is called implicit typecasting and where it is done from the user end is called explicit typecasting
In this approach, we generate the ASCII value of the given character with the help of a format specifier. We have stored the value of the given character inside a formal specifier by specifying the character to be an int. Hence, the ASCII value of that character is stored inside the format specifier.
Below is the Java program to implement the approach:
Java
// Java program to print ASCII Value of Character
// using format specifier
// Importing format library
import java.util.Formatter;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Character whose ASCII is to compute
char character = '}';
// Initializing the format specifier
Formatter formatSpecifier = new Formatter();
// Converting the character to integer and
// ASCII value is stored in the format specifier
formatSpecifier.format("%d", (int)character);
// Print the corresponding ASCII value
System.out.println(
"The ASCII value of the character ' "
+ character + " ' is " + formatSpecifier);
}
}
OutputThe ASCII value of the character ' } ' is 125
4. By Generating Byte
Steps to Print the ASCII Value are mentioned below:
- Initializing the character as a string.
- Creating an array of type byte by using getBytes() method.
- Printing the element at '0'th index of the bytes array.
This is the ASCII value of our character residing at the '0'th index of the string. This method is generally used to convert a whole string to their ASCII values. For the characters violating the encoding exception, the try-catch is given.
Below is the Java program to implement the approach:
Java
// Java program to print ASCII Value of Character
// by generating bytes.
// Importing I/O library
import java.io.UnsupportedEncodingException;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Try block to check exception
try {
// Character is initiated as a string
String sp = "}";
// An array of byte type is created
// by using getBytes method
byte[] bytes = sp.getBytes("US-ASCII");
/*This is the ASCII value of the character
/ present at the '0'th index of above string.*/
// Printing the element at '0'th index
// of array(bytes) using charAt() method
System.out.println("The ASCII value of "
+ sp.charAt(0) + " is "
+ bytes[0]);
}
// Catch block to handle exception
catch (UnsupportedEncodingException e) {
// Message printed for exception
System.out.println("OOPs!!!UnsupportedEncodingException occurs.");
}
}
}
OutputThe ASCII value of } is 125
Similar Reads
Java Program to Print the Elements of an Array
An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem
6 min read
Java Program to Print a New Line in String
Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, that is how to print a new line in a string using Java. Metho
3 min read
Java Program to illustrate the Usage of HexaDecimal
A decimal number is the sum of digits multiplied with a power of 10. Decimal numbers are represented with base 10 whereas hexadecimal Number is the sum of digits multiplied with the power of 16, somehow there are similar traits in the internal working of them just they are two different ways to pres
4 min read
Java Program To Write Your Own atoi()
The atoi() function in Java converts a numeric string into an integer. Unlike Javaâs built-in methods like Integer.parseInt(), we will implement our own version of atoi() with different levels of error handling and special case handling.Syntax: int myAtoi(String str);Parameter: str The numeric strin
6 min read
Java Program to Print Swastika Sign By Taking User Input
Take as input N, an odd number (>=5). Print the following pattern as given below for N = 7. Here the input format is the value of N and output results out in swastika pattern printing. Illustration: Input Format: Enter value of N ( >=5 ) Constraints: 5 <= N <= 99Output Format: Print the
4 min read
Java Program to Convert Long to String
The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a
4 min read
Java Program to Convert String to Long
To convert a String to Long in Java, we can use built-in methods provided by the Long class. In this article, we will learn how to convert String to Long in Java with different methods. Example:In the below example, we use the most common method i.e. Long.parseLong() method to convert a string to a
3 min read
Java Program to Convert Char to Int
Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s
4 min read
Java Program to Determine the Unicode Code Point at Given Index in String
ASCII is a code that converts the English alphabets to numerics as numeric can convert to the assembly language which our computer understands. For that, we have assigned a number against each character ranging from 0 to 127. Alphabets are case-sensitive lowercase and uppercase are treated different
5 min read
Java Program to Convert Binary to Octal
A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System
5 min read