在logcat中显示数组值



我有以下数组

final byte[] texttoprint = new byte[]{0x1b, 0x40, 0x1b,0x74,0x0D,
       (byte) 0x91,(byte) 0x92,(byte) 0x93,(byte) 0x94,(byte) 0x95,
       (byte) 0x96,(byte) 0x97,(byte) 0x98,(byte) 0x99,
       0x0A,0x0A,0x0A,0x0A,0x0A};

我想在Eclipse中将其值打印到logcat中,如下所示:

0x1b , 0x40

等等

我试过这个:

for (int index = 0; index < texttoprint.length;){
  Log.i("myactivity", String.format("%20x", texttoprint[index]));
}

但这是一个永无止境的循环打印1B。

使用此代码:

Log.i("myactivity", Arrays.toString(texttoprint));

它打印:[27, 64, 27...]

我哪里错了?

在循环中,还必须为循环中的每个过程增加索引。

for (int index = 0; index < texttoprint.length; index++){
 Log.i("myactivity", String.format("0x%20x", texttoprint[index]));
}

for (byte b: texttoprint){
 Log.i("myactivity", String.format("0x%20x", b));
}

打印日志。

            StringBuilder log = new StringBuilder();
            int i = 0;
            for (byte b : data.data) {
                i++;
                log.append(String.format("%02x", b));
                if (i % 4 == 0) {
                    log.append(" ");
                }
            }

最新更新