Pages

Sunday, September 20, 2015

Learn ASCII Code

What is It and Why Should I Care?

An "ASCII" is a data or text file that contains only characters coded from the standard ASCII character set. Characters 0 through 127 comprise the Standard ASCII Set and characters 128 to 255 are considered to be in the Extended ASCII Set. These codes, however, may not be the same in all computers and files containing these characters may not display or convert properly by another ASCII program.





Knowing something about ASCII can be helpful. ASCII files can be used as a common denominator for data conversions. For example, if program A can't convert its data to the format of program B, but both programs can input and output ASCII files, the conversion may be possible. Below are the examples convert integer to char and char to integer in java :


 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
   // convert char to ascii code ex: a = 97
   public static int CharToASCII(final char character) {
       return (int) character;
   }

   // convert ascii to char ex: 97 = a
   public static char ASCIIToChar(final int ascii) {
       return (char) ascii;
   }

   // listing all A-z in ascii
   public static void asciiList() {
       Vector < Character > vec = new Vector < Character > ();
       for (char c = 'A'; c <= 'z'; c++) {
           vec.add(c);
       }

       System.out.print(vec.toString());
   }

   // listting all int into ascii
   public static void asciiList2() {
       Vector < Character > vec = new Vector < Character > ();
       for (int c = 65; c <= 122; c++) {
           vec.add((char) c);
       }

       System.out.println(vec.toString());
   }

   //listing only alphabet
   public static void asciiList3() {
       Vector < Character > vec = new Vector < Character > ();
       for (int c = 65; c <= 122; c++) {
           // another way is by using Character.isLetter((char)c);
           // also can use regex expression ("a".matches ("[a-zA-Z]+\\.?"))
           if (((char) c >= 'a' && (char) c <= 'z') || ((char) c >= 'A' && (char) c <= 'Z'))
               vec.add((char) c);
       }
       System.out.println(vec.toString());
   }

   public static void main(String[] args) {

       System.out.println(CharToASCII('a'));
       System.out.println(ASCIIToChar(97));
       asciiList();
       asciiList2();
       asciiList3();
   }

No comments:

Post a Comment