Thinking in Java Fourth Edition Bruce Eckel Chapter 2 Exercise 13

Exercise 13: Write a method that displays char values in binary form. Demonstrate it using several different characters.

Solution:

public class CharBinaryTest
{
    public static void main(String [] args)
    {
        char c = 'a';
        System.out.println(Integer.toBinaryString(c));
        c = 'b';
        System.out.println(Integer.toBinaryString(c));
        c = 'c';
        System.out.println(Integer.toBinaryString(c));
        c = 'd';
        System.out.println(Integer.toBinaryString(c));
        c +=1;
        System.out.println(Integer.toBinaryString(c));
        c = 'A';
        System.out.println(Integer.toBinaryString(c));
        for(int i = 0; i < 26; i++)
        {
            c +=1;
            System.out.println(Integer.toBinaryString(c));
        }
    }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *