×

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday variable undertaking operations. To understand this situation, you ought to have an understanding of the subsequent Java programming subjects:

1. Java Date Type (Primitive)
2. Java Hello World Program

The full form of ASCII is American Standard Code Information Interchange. The ASCII code contain 7-bit characters. The ASCII code represent the numeric value of any given character. The 7-bit set contain 128 characters which is between 0 to 127.instance- The ASCII fee of H is seventy-two, ASCII value of B is 66. we can print the ASCII fee of man or woman A to Z, a to z, and special characters (@, #, $, ! and so on). To print the ASCII price, there are approaches.

To print the ASCII cost of a person, we need not apply any method or magnificence. Java programming language can convert the person's price internally to an ASCII value. allow observed the ASCII cost of a man or woman using the Java software program.

For print the ASCII value, there is two ways. These are-

  1. Assigning a Variable to the int Variable
  2. Using Type Casting

1. Assigning a Variable to the int Variable:

Now right here we speak approximately how we can assign a Variable to the int Variable. To print the ASCII price of a man or woman, we want no longer to use any method or magnificence. Java programming language can converts the individual fee internally to an ASCII cost. Permit found the ASCII fee of an individual the usage of the Java software.

In the below application, we have assigned characters a and b within the ch1 and ch2 variables, respectively. To find out the ASCII cost of a and b, we have set the ch1 and ch2 variables to the integer variables asciivalue1 and asciivalue2, respectively. ultimately, we've published the variables asciivalue1 and asciivalue2 in which ASCII values of the characters are stored.

Example 1 - Here we give an example of assign a Variable to the int Variable.

public class AsciiValue
{  
public static void main(String[] String)   
{  
//assigned two characters for found the ASCII value of those character 
int ch1 = 'A';  
int ch2 = 'B';  
//Print the result 
System.out.println("The ASCII value of A is: "+ch1);  
System.out.println("The ASCII value of B is: "+ch2);  
}  
}  

Output: For finding the result, we compile the above program and also run it.

The ASCII value of A is: 65
The ASCII value of B is: 66

Example 2 - Here we give an example of assign a Variable to the int Variable.

public class PrintAsciiValue  
{  
public static void main (String[] args)   
{  
//assigned two characters for found the ASCII value of those character  
char ch1 = 'H';  
char ch2 = 'k';  
// given variable stores the character’s integer value 
int asciivalue1 = ch1;  
int asciivalue2 = ch2;  
System.out.println("The ASCII value of " + ch1 + " is: " + asciivalue1);  
System.out.println("The ASCII value of " + ch2 + " is: " + asciivalue2);  
}  
}  

Output: For finding the result, we compile the above program and also run it.

The ASCII value of H is: 72
The ASCII value of K is: 75

2. Using Type Casting:

Now here we discuss about the how we can using the Type Casting. It is a manner to convert a variable into some other data. In the following software, we have declared variables ch1 and ch2 of kind char having the man or woman cost of a and b respectively. within the subsequent two lines, we have forged char type to int kind the use of (int). In the end, we have revealed the variables ascii1 and ascii2 where ASCII values of the characters are saved.

Example 1 - Here we give an example of using the Type Casting.

import java.util.Scanner;  
public class AsciiValue  
{  
public static void main(String args[])  
{  
System.out.print("Enter a character: ");  
Scanner sc = new Scanner(System.in);  
char chr = sc.next().charAt(0);  
int asciiValue = chr;  
System.out.println("ASCII value of " +chr+ " is: "+asciiValue);  
}  
}  

Output: For finding the result, we compile the above program and also run it.

Enter a character: r
The ASCII value of r is: 82

Example 2 - Here we give an example of using the Type Casting.

public class PrintAsciiValue
{
public static void main(String [] args)
{
//assigned two characters for found the ASCII value of those character  
char ch = ‘f’;
int ascii = ch;
int castAscii = (int) ch;
//Print the result
System.out.println(“The ASCII value of ” + ch + " is: "+asciiValue);  
System.out.println(“The ASCII value of ” + ch + " is: "+ castAscii);  
}
}

Output: For finding the result, we compile the above program and also run it.

The ASCII value of f is: 70
The ASCII value of f is: 70

So, right here we speak approximately the way to get ASCII fee of char in Java. We also give some examples of it and also provide the output of the given program for better understand.


Related Topics

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.

Finding middle node of a linked list in Java

To find the middle node of a linked list we have various methods in Java. Method 1 In this method two pointers are used, one of which advances quickly, and the other of...

6 minutes read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

String Concatenation in Java

In Java, it gathers a new String that combines several strings. Following are the manners to concatenate strings in Java: By + (String concatenation) operatorBy concat() method By + (String concatenation) operator Java...

4 minutes read.

Java vs Node.js

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Difference between String Tokenizer and split Method in Java

Introduction Today, let us understand the difference between String Tokenizer and Split Method. First, let us learn about the String Tokenizer and Split method individually and then know about their differences String...

7 minutes read.

Coin change problem in dynamic programming

In this tutorial, we will understand a popular problem called the coin changeproblem through dynamic programming. This problem checks the logical and critical thinking ability of the person. Dynamic Programming...

5 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Three Partition Problems in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

8 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.