1. 程式人生 > 其它 >位元組陣列按二進位制字串列印輸出

位元組陣列按二進位制字串列印輸出

 public static void main(String[] args) throws UnsupportedEncodingException {
        String c = "中";
        byte[] bs = c.getBytes("GBK");
        for (int i = 0; i < bs.length; i++) {
            System.out.print(byteToBit((byte) bs[i]));
            System.out.print(" "
); } } // 把位元組轉換成bit字串 public static String byteToBit(byte b) { return "" + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1) + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1) + (byte) ((b >> 3) & 0x1
) + (byte) ((b >> 2) & 0x1) + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1); }

上例的“中”的輸出結果:11010110 11010000