View RSS Feed

My Java Tips

Handling ASCII character set in Java

Rate this Entry
by , 11-19-2011 at 06:11 PM (4813 Views)
In this post, I will talk about ASCII character set and how to deal with ASCII codes in Java.


ASCII character uses 7 bits for character representation. This means that there are total of 128 characters supported by ASCII set. Point to note is that there are only 93 printable characters in ASCII whose ASCII codes are from 33 till 126.

Java supports all popular character including UTF. But sometimes you only want the input to comprise of ASCII printable characters. This may be useful if your business logic does not allow any other character set.

Lets first see all the printable characters in ASCII character set.

Java Code:
for (int i=33;i<=126;i++)
System.out.println("Code: " + i + " Character: " +(char)i);
Output:


Code: 33 Character: !
Code: 34 Character: "
Code: 35 Character: #
Code: 36 Character: $
Code: 37 Character: %
Code: 38 Character: &
Code: 39 Character: '
Code: 40 Character: (
Code: 41 Character: )
Code: 42 Character: *
Code: 43 Character: +
Code: 44 Character: ,
Code: 45 Character: -
Code: 46 Character: .
Code: 47 Character: /
Code: 48 Character: 0
Code: 49 Character: 1
Code: 50 Character: 2
Code: 51 Character: 3
Code: 52 Character: 4
Code: 53 Character: 5
Code: 54 Character: 6
Code: 55 Character: 7
Code: 56 Character: 8
Code: 57 Character: 9
Code: 58 Character: :
Code: 59 Character: ;
Code: 60 Character: <
Code: 61 Character: =
Code: 62 Character: >
Code: 63 Character: ?
Code: 64 Character: @
Code: 65 Character: A
Code: 66 Character: B
Code: 67 Character: C
Code: 68 Character: D
Code: 69 Character: E
Code: 70 Character: F
Code: 71 Character: G
Code: 72 Character: H
Code: 73 Character: I
Code: 74 Character: J
Code: 75 Character: K
Code: 76 Character: L
Code: 77 Character: M
Code: 78 Character: N
Code: 79 Character: O
Code: 80 Character: P
Code: 81 Character: Q
Code: 82 Character: R
Code: 83 Character: S
Code: 84 Character: T
Code: 85 Character: U
Code: 86 Character: V
Code: 87 Character: W
Code: 88 Character: X
Code: 89 Character: Y
Code: 90 Character: Z
Code: 91 Character: [
Code: 92 Character:
Code: 93 Character: ]
Code: 94 Character: ^
Code: 95 Character: _
Code: 96 Character: `
Code: 97 Character: a
Code: 98 Character: b
Code: 99 Character: c
Code: 100 Character: d
Code: 101 Character: e
Code: 102 Character: f
Code: 103 Character: g
Code: 104 Character: h
Code: 105 Character: i
Code: 106 Character: j
Code: 107 Character: k
Code: 108 Character: l
Code: 109 Character: m
Code: 110 Character: n
Code: 111 Character: o
Code: 112 Character: p
Code: 113 Character: q
Code: 114 Character: r
Code: 115 Character: s
Code: 116 Character: t
Code: 117 Character: u
Code: 118 Character: v
Code: 119 Character: w
Code: 120 Character: x
Code: 121 Character: y
Code: 122 Character: z
Code: 123 Character: {
Code: 124 Character: |
Code: 125 Character: }
Code: 126 Character: ~

So now we know the printable characters in ASCII character set.


As I have already written, ASCII has only 93 printable characters with ASCII codes from 33 till 126 (inclusive). Lets see how to validate input according to ASCII printable characters.

I have written a method that can be used for validation:

Java Code:
public static ArrayList valiateCharacterSetAscii(ArrayListfileContents){

ArrayList tmpList = new ArrayList();
String str = "";

for(int j=0; j < fileContents.size();j++)
{
str = fileContents.get(j);
String resultantString = "";
for(int i=0;i
int charCode = (int)str.charAt(i);
if( ((charCode>=33 && charCode <=126)))
resultantString += str.charAt(i);
else
resultantString += null;
}
tmpList.add(resultantString);
}
return tmpList;
}
The method above replaces non ASCII printable characters with null. You may replace null with any character.
Now lets use this one to see if it really works:

Java Code:
ArrayList list = new ArrayList();
list.add("ABCDEF123");
list.add("-_-#*");
list.add("όδ");
list = valiateCharacterSetAscii(list);
System.out.println(list);
I created an ArrayList with strings. I added some non ASCII characters for testing. I called validateCharacterSetAscii(…) method and then printed the resultant ArrayList.

Output:


[ABCDEF123, -_-#*, nullnull]

The output is as expected. Non ASCII printable characters are replaced by null.

Happy coding.

Submit "Handling ASCII character set in Java" to Facebook Submit "Handling ASCII character set in Java" to Digg Submit "Handling ASCII character set in Java" to del.icio.us Submit "Handling ASCII character set in Java" to StumbleUpon Submit "Handling ASCII character set in Java" to Google

Comments