-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAscii.txt
136 lines (111 loc) · 4.05 KB
/
Ascii.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
PrintAsciiValueExample1.java
public class PrintAsciiValueExample1
{
public static void main(String[] args)
{
// character whose ASCII value to be found
char ch1 = 'a';
char ch2 = 'b';
// variable that stores the integer value of the character
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:
The ASCII value of a is: 97
The ASCII value of b is: 98
Another way to write the above program is:
-------------------------------------------------------------------------------------------------------------------------------
PrintAsciiValueExample2.java
public class PrintAsciiValueExample2
{
public static void main(String[] String)
{
int ch1 = 'a';
int ch2 = 'b';
System.out.println("The ASCII value of a is: "+ch1);
System.out.println("The ASCII value of b is: "+ch2);
}
}
Output:
The ASCII value of a is: 97
The ASCII value of b is: 98
Similarly, we can print the ASCII value of other characters (A, B, C, …., Z) and symbols (!, @, $, *, etc.).
Using Type-Casting
Type-casting is a way to cast a variable into another datatype.
In the following program, we have declared two variables ch1 and ch2 of type char having the character a and b, respectively. In the next two lines, we have cast char type to int type using (int). After executing these two lines, the variable ch1 and ch2 are converted to an int variable ascii1 and ascii2, respectively.
Finally, we have printed the variable ascii1 and ascii2 in which ASCII values of the characters are stored.
------------------------------------------------------------------------------------------------------------------
PrintAsciiValueExample3.java
public class PrintAsciiValueExample3
{
public static void main(String[] args)
{
//characters whose ASCII value to be found
char ch1 = 'a';
char ch2 = 'b';
//casting or converting a charter into int type
int ascii1 = (int) ch1;
int ascii2 = (int) ch2;
System.out.println("The ASCII value of " + ch1 + " is: " + ascii1);
System.out.println("The ASCII value of " + ch1 + " is: " + ascii2);
}
}
Output:
The ASCII value of a is: 97
The ASCII value of b is: 98
If we do not want to assign character, we can also take a character from the user.
--------------------------------------------------------------------------------------------------------------------------
PrintAsciiValueExample4.java
import java.util.Scanner;
public class PrintAsciiValueExample4
{
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 1:
Enter a character: P
ASCII value of P is: 80
Output 2:
Enter a character: G
ASCII value of G is: 71
The following program prints the ASCII value (0 to 255) of all the characters. In the output, we have shown a few values.
AD
-------------------------------------------------------------------------------------------------------------------------------------------
AsciiValueOfAllChracters.java
public class AsciiValueOfAllChracters
{
public static void main(String[] args)
{
for(int i = 0; i <= 255; i++)
{
System.out.println(" The ASCII value of " + (char)i + " = " + i);
}
}
}
Output:
How to Print ASCII Value in Java
If we want to print the ASCII value of all the alphabets (A to Z), we can set the values in the loop and print them.
-----------------------------------------------------------------------------------------------------------------------------------------------
AsciiValueAtoZ.java
public class AsciiValueAtoZ
{
public static void main(String[] args)
{
for(int i = 65; i <= 90; i++)
{
System.out.println(" The ASCII value of " + (char)i + " = " + i);
}
}
}
Output:
How to Print ASCII Value in Java
Similarly, we can print the ASCII value of a to z by changing the loop in the above code.