Java get char from ascii

How To Find or Get ASCII Value of a Character in Java

ASCII stands for American Standard Code for Information Interchange. In the C programming language, you have learned to work with 7 bit ASCII characters. Each character has a unique ASCII value which is a whole number or integer. Java programming language uses UNICODE to represent character data. Unicode takes 16 bits to represent each character. It covers almost all characters of all languages around the world. Let us learn how to find or get the ASCII value of a character in Java.

Get or Find ASCII Value of a Character in Java / Convert a Number to Character

Java language supports implicit conversion of data from one data type to another data type. CHAR data type is implicitly converted to INT or SHORT data type. Explicit Typecasting is also used to convert a number or integer to its character equivalent. ASCII value is nothing but a number (integer) that uniquely identifies a character in Java or C languages. The below example Java program prints ASCII values of all keyboard characters for your reference. So, you can create your own ASCII table using Java.

Using a FOR loop, we built the whole ASCII value table using int to char conversions. The above program prints ASCII values of all Alphabets, Numbers, Symbols and Punctuation Marks.

ASCII Table for Reference

ASCII Character Escape Character Meaning
0 NULL \0 Null
1 SOM Start of Heading
2 EOA Start of Text
3 EOM End of Text
4 EOT End of Transmission
5 WRU Enquiry
6 RU Acknowledgement
7 BELL \a Bell
8 FE0 \b Backspace
9 HT/SK \t Horizontal Tab
10 LF \n Line Feed
11 VTAB \v Vertical Tab
12 FF \f Form Feed
13 CR \r Carriage Return
14 SO Shift Out
15 SI Shift In
16 DC0 Data Link Escape
17 DC1 Device Control 1 (often XON)
18 DC2 Device Control 2
19 DC3 Device Control 3 (often XOFF)
20 DC4 Device Control 4
21 ERR Negative Acknowledgement
22 SYNC Synchronous Idle
23 LEM End of Transmission Block
24 S0 Cancel
25 S1 End of Medium
26 S2 Substitute
27 S3 \e Escape
28 S4 File Separator
29 S5 Group Separator
30 S6 Record Separator
31 S7 Unit Separator
32 space
33 !
34 «
35 #
36 $
37 %
38 &
39
40 (
41 )
42 *
43 +
44 ,
45
46 .
47 /
48 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
58 :
59 ;
60
61 =
62 >
63 ?
64 @
65 A
66 B
67 C
68 D
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
91 [
92 \
93 ]
94
95
96 `
97 a
98 b
99 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
107 k
108 l
109 m
110 n
111 o
112 p
113 q
114 r
115 s
116 t
117 u
118 v
119 w
120 x
121 y
122 z
123
124 |
125 >
126 ~
127 DEL

This is how we try to get or find or print the ASCII value of a character. We also did the conversion of a number to its character equivalent.

Share this article with your friends and colleagues to encourage authors.

Источник

How to Convert an ASCII Code to char in Java?

ASCII is the abbreviation of “American Standard Code for Information Interchange”. A computer knows the language in numeric form. Therefore, ASCII is used to communicate with computers by exchanging information. All keyboard characters, including all alphabets, numbers, and special characters, comprise a unique ASCII code that the computer understands to process the typed key.

This blog will discuss converting an ASCII code to a character in Java.

How to Convert an ASCII Code to char in Java?

For converting an ASCII code to a character in Java, there are different methods listed below:

Let’s check the functionality of each of these methods with examples.

Method 1: To Convert an ASCII Code to char Using Type Casting

Most programmers utilize Type Casting for converting an ASCII code to char in a Java program as it directly converts one data type to another.

Syntax
The syntax for converting ASCII Code to char in Java using the Type Casting method is given as:

ascii” is the variable that stores a value of data type “int”. The keyword “char” with the parenthesis like “(char)” indicates that the mentioned int type “ascii” variable is typecasted into a character, and the resultant value will be stored in “asciiToChar”.

Let’s check out an example to understand the conversion of ASCII code to char using Type Casting.

Example
Here, we have an integer type variable “ascii” initialized with “69”:

Now, we will convert the created variable to a character using Type Casting:

Lastly, we will print the resultant character “ascii”:

The output indicates that the ASCII code “69” is converted to “E” char:

Let’s check some other methods to convert the ASCII code to char in Java.

Method 2: To Convert an ASCII Code to char Using toString()

The Java wrapper class named “Character” also offers a “toString()” method, which allows us to convert an ASCII code to the string representing the character’s value.

Here, “ascii” is an “int” type data variable containing ASCII code that will be converted to a string referring to the corresponding character.

Example
In this example, we have an ASCII value “75” stored in “ascii”:

We will call the “Character.toString()” method by passing the created character as a parameter and then store the returned value in “asciiToChar” String type variable. Now the question is why it is a String type variable, not a character type? Because the toString() method will always return a string:

Lastly, execute the “System.out.println()” method to print out the required value:

As you can see, the given program successfully converted “75” ASCII code to the “K” character:

We have one more method to perform the same operation. So, move to the next section!

Method 3: To Convert an ASCII Code to char Using toChars()

The “toChars()” method of the Character wrapper class can also be utilized to convert an ASCII code to char in a Java program. It returns the output as an array of characters.

Here, “ascii” is an integer type variable having ASCII code that is passed to the “Character.toChars()” method. This method will return a character array.

Example
Firstly, we will create a variable named “ascii” having “116” as ASCII code:

Then, we will call the “Character.toChars()” method, pass “ascii” as an argument, and store the returned char array in “asciiToChar”:

Lastly, we will print the output on the console:

System . out . print ( «Ascii » + ascii + » is a value of Character: » ) ;
System . out . println ( asciiToChar ) ;

We presented the easiest methods to convert ASCII code to char in Java.

Conclusion

To convert ASCII code to a character, you can use different methods such as Type Casting, toString() method, and toChars() method of the Character class. The toString() method will return a character as a String, while the toChars() method returns the array of characters. Type Casting is the most common and easy method to convert an ASCII code to a character, as it directly typecasts the ASCII code to char. This blog discussed the methods used to convert an ASCII code to char in Java.

About the author

Farah Batool

I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.

Источник

Convert an ASCII Code to Char

Convert an ASCII Code to Char

  1. Use Casting to Convert ASCII to Char in Java
  2. Use Character.toString to Convert ASCII to Char in Java
  3. Use Character.toString to Convert ASCII to Char in Java
  4. Use Character.toChars() to Convert ASCII to Char in Java

This article discusses how you can convert an ASCII code into its character using methods in Java. Additionally, we demonstrate how you can change upper-case alphabets into lower-case, and vice versa.

Use Casting to Convert ASCII to Char in Java

The most basic and easy way to extract the character from an ASCII Code is to cast the ASCII code to a char directly; this will convert the asciiValue of the int type to a char type.

public class Main   public static void main(String[] args)    int asciiValue = 97;   char convertedChar = (char)asciiValue;  System.out.println(convertedChar);   > > 

Use Character.toString to Convert ASCII to Char in Java

The character class of Java provides us with a toString() method, which is converted to a char in a codePoint; in this case, we have an ASCII code. We can put the conversion method into a loop to get all the uppercase English alphabets. Note that the loop goes from 65 to 90, which are the codes corresponding to the uppercase alphabets.

This method’s benefit from the example we used above is that it can throw an exception if the int value is not validated correctly.

public class Main   public static void main(String[] args)    int asciiValue = 65;   for(int i = asciiValue; i  90; i++)  String convertedChar = Character.toString(i);  System.out.println(i+" => "+convertedChar);  >   > > 
65 => A 66 => B 67 => C 68 => D 69 => E 70 => F 71 => G 72 => H 73 => I 74 => J 75 => K 76 => L 77 => M 78 => N 79 => O 80 => P 81 => Q 82 => R 83 => S 84 => T 85 => U 86 => V 87 => W 88 => X 89 => Y 90 => Z 

Use Character.toString to Convert ASCII to Char in Java

To convert the ASCII codes to lower-case alphabets, we only need to change the loop range; it should start with 97 and end at 122.

public class Main   public static void main(String[] args)    int asciiValue = 97;   for(int i = asciiValue; i  122; i++)  String convertedChar = Character.toString((char)i);  System.out.println(i+" => "+convertedChar);  >   > > 
97 => a 98 => b 99 => c 100 => d 101 => e 102 => f 103 => g 104 => h 105 => i 106 => j 107 => k 108 => l 109 => m 110 => n 111 => o 112 => p 113 => q 114 => r 115 => s 116 => t 117 => u 118 => v 119 => w 120 => x 121 => y 122 => z 

Use Character.toChars() to Convert ASCII to Char in Java

We can use another method of the character class in Java, which is toChars ; it takes a codePoint like the ASCII value and returns an array of char .

public class Main   public static void main(String[] args)    int asciiValue = 255;   char[] convertedCharArray = Character.toChars(asciiValue);   System.out.println(convertedCharArray);   > > 

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

Related Article — Java ASCII

Related Article — Java Char

Copyright © 2023. All right reserved

Источник

Java program to convert a character to ASCII code and vice versa

Java Program to convert a string or char to ASCII value

Computers can only understand numbers, more specifically, binaries ( 0 or 1 ). To store or exchange text-based information in computers, we need some kind of character encoding that can encode text data to numbers which can then be represented using binary digits.

ASCII (American Standard Code for Information Exchange) is a character encoding standard developed in the early 60’s for representing and exchanging text-based information in computers. The ASCII character set consists of 128 characters that are represented using 7-bit integers. You can check out the complete ASCII character set here.

This article shows you how to convert a character to ASCII code or get the character from a given ASCII code in Java. The conversion from character to ASCII code or ASCII code to character is done using type casting. Check out the following programs to know more:

Convert character to ASCII code in Java

class CharToAsciiCodeExample  public static void main(String[] args)  char ch = 'A'; int asciiValue = ch; // char is automatically casted to int System.out.printf("Ascii Value of %c = %d\n", ch, asciiValue); > >
$ javac CharToAsciiCodeExample.java $ java CharToAsciiCodeExample Ascii Value of A = 65

Convert ASCII code to character in Java

class AsciiCodeToCharExample  public static void main(String[] args)  int asciiValue = 97; char ch = (char) asciiValue; System.out.printf("Character corresponding to Ascii Code %d = %c\n", asciiValue, ch); > >
$ javac AsciiCodeToCharExample.java $ java AsciiCodeToCharExample Character corresponding to Ascii Code 97 = a

Источник

Читайте также:  Get variable javascript url
Оцените статью