I wrote up this piece of code.
public static void printHex(byte[] A){
String Digs = "0123456789ABCDEF";
for(int i = 0;i<A.length;i++){
byte a = A[i];
int ByteVal = a + 128;
int L = ByteVal % 16;
int H = ByteVal / 16;
String O = Digs.substring(H, H+1) + Digs.substring(L, L+1);
System.out.print(O);
}
System.out.println("");
}
This is similar to code I've written in C before, except in C I would write.
int L = ByteVal >> 4;
int H = ByteVal & 15;
To take advantage of power of two thingies :)

Signature
"It's better to have rocked and lost than never to have rocked at
all." -John Flansburgh
Chris Uppal - 22 Oct 2005 15:31 GMT
> byte a = A[i];
> int ByteVal = a + 128;
> int L = ByteVal % 16;
> int H = ByteVal / 16;
Can be replaced by:
int b = A[i];
int lo = b & 0xF;
int hi = (b >> 4) & 0xF;
(Just the same sort of bit-twiddling as you'd do in C.)
> String O = Digs.substring(H, H+1) + Digs.substring(L, L+1);
> System.out.print(O);
Can be replaced by
String digits = "01...DEF"; // as before
System.out.print(digits.charAt(lo));
System.out.print(digits.charAt(hi));
You could use a static char[] array instead of the digits String, but then
you'd have to mess around declaring a static variable and initialising it.
Using a String is simpler and probably not measurably less efficient.
BTW, it's generally a bad idea to use "odd" names for variables -- even local
variables -- and names beginning with capital letters are very odd indeed for
anything except the name of a class/interface or a constant (static final
field).
-- chris
Luc The Perverse - 22 Oct 2005 20:06 GMT
>> byte a = A[i];
>> int ByteVal = a + 128;
[quoted text clipped - 28 lines]
> anything except the name of a class/interface or a constant (static final
> field).
Thank you - I like your suggestions. Your code "looks" more correct.
As for the variable names - I guess old habits die hard.

Signature
"It's better to have rocked and lost than never to have rocked at
all." -John Flansburgh
Pete Barrett - 23 Oct 2005 09:37 GMT
>I wrote up this piece of code.
>
> public static void printHex(byte[] A){
> String Digs = "0123456789ABCDEF";
You might try using Integer.toHexString(...) (static method) to
translate the integer instead of using low and high bytes separately
as indices into the array of digits. You do have to be a bit careful
about leading 0s (it doesn't put them in) and about sign extension
when casting from Byte to Integer. So:
int a;
String o;
for (int i = 0; i < array.length; i++){
a = ((int)array[i]) && 0xFF; // deals with sign extension
a |= 0x100; // ensures form is '1xx'
o = Integer.toHexString(a).substring(1); // removes leading 1
System.out.print(o);
}
You may feel it's less clear, though.
Pete Barrett
Roedy Green - 23 Oct 2005 11:03 GMT
>This is similar to code I've written in C before, except in C I would write.
>
> int L = ByteVal >> 4;
> int H = ByteVal & 15;
>
>To take advantage of power of two thingies :)
See http://mindprod.com/jgloss/binary.html
Java has the same bit fiddling powers as C.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.