0

我有一个字符数组

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,
};

如何将其打印为 8 位二进制文​​件?

4

2 回答 2

3

用于toBinaryString每个项目:

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 + " ");
 }

输出

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

于 2013-04-23T07:44:00.133 回答
0

如果你输出是0000000000000000f848484...然后

    for(char c : myArray) {
        System.out.printf("%02x", (int)c);
    }
于 2013-04-23T07:46:13.733 回答