Outdated character encoding

I was busy with the source code for an old Java game written in the early nineties. If I remember correctly, it is written for JDK 1.1.

Somewhere in the code, int primitives (ranging from 0 to 120) are converted to characters. Here is an example:

char c = (char)(i+32);

This causes a problem for ints greater than 95. Here is the code and part of the output from the test case:

for(int i = 120; i >= 0; i--)
   System.out.println(i + " -> " + (char)(i+32));

Conclusion:

...
100 -> ?
99 -> ?
98 -> ?
97 -> ?
96 -> ?
95 -> 
94 -> ~
93 -> }
92 -> |
91 -> {
90 -> z
89 -> y
88 -> x
87 -> w
...
3 -> #
2 -> "
1 -> !
0 ->  

The integer value seems to be lost, since the index passes through the boundaries of the normal values โ€‹โ€‹of characters.

This, apparently, is the main cause of the error on the client side of the game user interface. This encoded integer is sent back to the client, which then performs the inverse operation (subtracting 32 from char and casting to get int back).

, '?' , '?' 95.

  • ?
  • -?
  • , , ?
+3
4

char Java - 16- . , int , , , , , (.. new String(byteArrayData, "ASCII")).

, , Java -128 + 127. ascii ( > 127), 256 int > 127, . : int ? .

+1

, .

, "" - .

?

, "" .

-?

, , , "?"

, , ?

, , , . , - , .

, , , . , , , . , , , , .

+2

, , ?

-, , ; , . () .

- , , ; .

char[] map = new char[128];  // ... or whatever the limit is 
for (int i = 0; i < 96; i++) {
    map[i] = (char) (i + 32);
}
// fill the rest of the array with suitable Unicode characters.
map[96] = ...
map[97] = ...

:

char c = (i >= 0 && i < 128) ? map[i] : '?'; // ... or throw an exception.
+1

All Articles