Aida Fariha
3 min readJun 24, 2020

ASCII Table in Java Programming

When I was still a junior developer, my boss once gave me a test. It was a short test, given to me in the morning and he expect for me to finish it by afternoon. It was a simple lab test to convert alphanumeric (PA123458) into certain condition using few formulas but the code must be as simple as possible.

The question was very straightforward and simple. But what interest me the most was this part:

Replace alphanumeric with digit based on formula below:

A, K, U replace with 0
B, L, V replace with 1
C, M, W replace with 2
D, N, X replace with 3
E, O, Y replace with 4
F, P, Z replace with 5
G, Q replace with 6
H, R replace with 7
I, S replace with 8
J, T replace with 9

At first glance, all I can think of is a switch case condition. But you can see that all characters actually have pattern from the ASCII table.

I proceed with the switch case due to time constraint and I can’t think of any better way. Here what it looks like.

It was straightforward. If the character matches, then the new value will overwrite the character. But it was actually quite long. Imagine if you have more characters than this. Not a very good solution, isn’t it?

There’s actually an easier, simpler way which is using ASCII table. Based on the ASCII table above, you can see that each character has the same last digit. For example, A, K and U represents 65, 75, and 85 simultaneously.

A, K, U = 65, 75, 85
B, L, V = 66, 76, 86
C, M, W = 67, 77, 87
D, N, X = 68, 78, 88
E, O, Y = 69, 79, 89
F, P, Z = 70, 80, 90
G, Q = 71, 81
H, R = 72, 82
I, S = 73, 83
J, T = 74, 84

If the number ends with 5, replaced the whole char with 0. The rest is as below.

In the end, it looks cleaner and simpler like this.

To find the ASCII value, I just need to assign char to int, and I straight away modulo the ASCII value to get the end digit, let say in this case, I used R. If the finalDigit is 5 then count must be 0, which also can be used as the replace value. So I used the count to loop and it will loop until the finalDigit is equals to lastDigit.

Need to take into account that the finalDigit will hit 0 again after 9 instead of 10, so I just assign conditional statement for the finalDigit to start at 0 again if it hits 10.

Output

This is actually a very basic programming and I was hesitating whether to write this as my first content or not. But to be honest, I never thought I would actually use the ASCII table because I thought the switch case is good and short enough. Slightly triggered when my boss said “You can do better” and that trigger makes me try again (and he’s actually the one who told me about this).

Thank you for reading my first post. If there’s any better solution feel free to share.