简体   繁体   中英

Displaying array values in the logcat

I have the following array

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

and I want to print its values to the logcat in Eclipse, like this:

0x1b , 0x40

and so on.

I tried this:

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

But that does an never ending loop printing 1B.

With this code:

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

it prints: [27, 64, 27...]

Where am I wrong?

In your loop you must also increment the index for each pass in the loop.

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

or

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

print log.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM