问题
I wanted to convert ASCII values to its corresponding characters so I wrote this simple code:
public class Test {
public static void main(String[] args) {
int i=0;
char ch='c';
for(i=0;i<127;i++)
{
ch=(char)i;
System.out.print(ch+"\t");
}
System.out.println("finish");
}
}
But as output it's showing nothing and along with that the control is not even getting out of the loop though the process gets finished..plz explain this kind of behavior and the right code.
回答1:
As other people have pointed out, you have included the control characters; if you alter the loop (as below) you get the full set, excluding these control characters:
public static void main() {
for(int i = 33; i < 127; i++)
{
char ch = (char) i;
System.out.print(i + ":" + ch + "\t");
}
System.out.println("finish");
}
来源:https://stackoverflow.com/questions/37297869/ascii-conversion