Writing a program to print the ASCII values of all 256 characters.
int digit[] = new int[256];
char array[] = new char[256];
for(int i=0;i<array.length;i++)
{
array[i] = (char) digit[i];
}
for(int i=0;i<array.length;i++)
{
System.out.println(array[i]);
}
I get a blank output when I run this code.
You do not need int digit[]
. You only need to loop from 0 to 255 and cast each int
to a char.
for (int i = 0; i < 256; i++) {
array[i] = (char) i;
}
Alternatively, you can write a for
loop using char
.
for (char i = 0; i < 256; i++) {
System.out.println(i);
}
Since you didn't initialize them, all the elements in digit
are 0
, and ASCII 0
is the null character.
TO be honest, you don't need an array there - just iterate over them numbers 0 to 255 and print their cast to char
s:
for (int i = 0; i < 256; ++i) {
System.out.println(i + " -- " + ((char)i)));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.