Question

I've a char array

static char[] myArray  ={   
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x48, 0x48, 0x48, 0xb0, 0x00, 0xc0, 0x20,
0x20, 0x20, 0xc0, 0x00, 0xc0, 0x20, 0x20, 0x20, 0xc0, 0x00, 0x40, 0xa0, 0xa0, 0xa0, 0x20, 0x00,
0x00, 0x20, 0xf0, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0xf8, 0x08,
};

How could I print it as 8-bit binary ?

Was it helpful?

Solution

Use toBinaryString for each item:

for (int i = 0; i < myArray.length; i++) {
        String b = Integer.toBinaryString(myArray[i]);

        if (b.length() < 8) {
            b = "000000000".substring(0, 8 - b.length()).concat(b);
        } else {
            b = b.substring(b.length() - 8);
        }

        System.out.print(b + " ");
 }

Output

00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 11111000 01001000 01001000 01001000 1011000 ...

OTHER TIPS

if you output to be 0000000000000000f848484... then

    for(char c : myArray) {
        System.out.printf("%02x", (int)c);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top