Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASCII conversion

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.

like image 698
Siddhartha Choudhury Avatar asked Apr 07 '26 09:04

Siddhartha Choudhury


1 Answers

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");
}
like image 160
Sean Avatar answered Apr 13 '26 06:04

Sean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!