1

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
kosbou
  • 3,919
  • 8
  • 31
  • 36

2 Answers2

4

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));
}
Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
0

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(" ");
                }
            }
lingyfh
  • 1,363
  • 18
  • 23