ASCII value is the representation of number and Char in Decimal or Hexadecimal. Like ASCII value of capital ‘A’ is 065 and small ‘a’ is 097 Similarly there is ASCII value of each char in small or Capital.
Can we find ASCII values using Java?
Yes, I am going to show some examples in Java to find the ASCII value of char in small (a-z ) letters and capital letters(A-Z).
Find the ASCII value of a char in Java.
It’s really simple to find the ASCII value of a char in Java. We just need to convert the char into an int. What does this mean? Let’s try to understand by a simple example?
We have a char ch = ‘A’. Here ch is a variable that holds a char ‘A’. Now if we change the type of char ‘A’ char to int then it will print an ASCII value of the char.
char ch = 'A'; int n = ch; System.out.println("Ascii Value of "+ch+" will be "+n);
In this way, we can find the ASCII value of any char in Java. In the below code, we just need to change the value of the variable ch.
Java code to find ASCII value of any char
package com.demo; import java.util.*; class Main { public static void main(String[] args) { char Char= 'B'; System.out.println("ASCII Value of Char: "+Char+0); } }
Find ASCII value of Capital A-Z in Java
In this example, we will find ASCII value-form A-Z. So to solve this problem the will follow two main step
- Convert the char into an int.
- Run a loop from A-Z.
package com.demo; import java.util.*; class Main { public static void main(String[] args) { char CapitalChar = 'A'; char SmallChar = 'a'; //Find the ASCII Value from A-Z System.out.println("ASCII Value from 'A' to 'Z'"); for(int i ='A'; i<='Z'; i++) { System.out.println(CapitalChar +" : "+i); CapitalChar++; } } }
Output
Find ASCII value of small a-z in Java
In this example, we will find ASCII value-form a-z. So to solve this problem the will follow two main step
- Convert the char into an int.
- Run a loop from a-z.
package com.demo3; public class Main { public static void main(String[] args) { char SmallChar = 'a'; System.out.println("ASCII Value from 'a' to 'z'"); for (int i = 'a'; i <= 'z'; i++) { System.out.println(SmallChar + " : " + i); SmallChar++; } } }
Output
ASCII value of 0 to 9 in java
In this example, we will find ASCII value-form 0-9. So to solve this problem the will follow two main steps. Here, we will just need to add 48 after each iteration(because the ASCII code of numbers starts from 48)
- Run a loop from 0-9.
- Add 48 for each iteration.
package com.demo3; public class Main { public static void main(String[] args) { System.out.println("ASCII Value from 0 to 9"); for(int i = 0; i <= 9; i++) { System.out.println(i+" "+(i+48)); } } }
Output
Find ASCII value of special characters in java
In this example, we will find ASCII value-form space-forward slash(/). So to solve this problem the will follow two main step
- Convert the char into an int.
- Run a loop from ‘ ‘ to ‘/’.
package com.demo3; public class Main { public static void main(String[] args) { char specialChar = ' '; System.out.println("ASCII Value from ' ' to '/'"); for (int i = ' '; i <= '/'; i++) { System.out.println(specialChar + " : " + i); specialChar++; } } }
Output
Java Program to find ASCII value of each digit of a given number.
In this example, we will pass a number as an input and we should get the ASCII value of each digit. Following steps we would use:
- Input a number from the user.
- Iterate over the number till it is greater than 0.
- Add 48(because the ASCII code of numbers starts from 48) to each digit by extracting each digit after each iteration.
- Print the ASCII value.
package com.demo3; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); while(n > 0){ int rem = n % 10; System.out.println("ASCII value of "+rem+ " " +(rem+48)); n = n /10; } } }
Output
ASCII Table
The following table shows the detailed view of ASCII codes of numbers.
Dec = Decimal Value Char = Character '5' has the int value 53 if we write '5'-'0' it evaluates to 53-48, or the int 5 if we write char c = 'B'+32; then c stores 'b' Dec Char Dec Char Dec Char Dec Char --------- --------- --------- ---------- 0 NUL (null) 32 SPACE 64 @ 96 ` 1 SOH (start of heading) 33 ! 65 A 97 a 2 STX (start of text) 34 " 66 B 98 b 3 ETX (end of text) 35 # 67 C 99 c 4 EOT (end of transmission) 36 $ 68 D 100 d 5 ENQ (enquiry) 37 % 69 E 101 e 6 ACK (acknowledge) 38 & 70 F 102 f 7 BEL (bell) 39 ' 71 G 103 g 8 BS (backspace) 40 ( 72 H 104 h 9 TAB (horizontal tab) 41 ) 73 I 105 i 10 LF (NL line feed, new line) 42 * 74 J 106 j 11 VT (vertical tab) 43 + 75 K 107 k 12 FF (NP form feed, new page) 44 , 76 L 108 l 13 CR (carriage return) 45 - 77 M 109 m 14 SO (shift out) 46 . 78 N 110 n 15 SI (shift in) 47 / 79 O 111 o 16 DLE (data link escape) 48 0 80 P 112 p 17 DC1 (device control 1) 49 1 81 Q 113 q 18 DC2 (device control 2) 50 2 82 R 114 r 19 DC3 (device control 3) 51 3 83 S 115 s 20 DC4 (device control 4) 52 4 84 T 116 t 21 NAK (negative acknowledge) 53 5 85 U 117 u 22 SYN (synchronous idle) 54 6 86 V 118 v 23 ETB (end of trans. block) 55 7 87 W 119 w 24 CAN (cancel) 56 8 88 X 120 x 25 EM (end of medium) 57 9 89 Y 121 y 26 SUB (substitute) 58 : 90 Z 122 z 27 ESC (escape) 59 ; 91 [ 123 { 28 FS (file separator) 60 < 92 \ 124 | 29 GS (group separator) 61 = 93 ] 125 } 30 RS (record separator) 62 > 94 ^ 126 ~ 31 US (unit separator) 63 ? 95 _ 127 DEL
Key Point in about Char and ASCII values in Java
- The char range lies between 0 to 65,535 (inclusive).
- Its default value of char in java is ‘\u0000’.
- char size in Java is 2 bytes.
- It is used to store characters.
Thus, in this way, this was all about ASCII Value in Java and how we can find ASCII values of number, characters, and etc.