我有一个char数组

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位二进制文​​件?

最佳答案

对每个项目使用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 ...

关于java - 在Java Char数组中以二进制形式打印,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16163661/

10-12 02:32