Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Better way to do this?  PrintHex function

Thread view: 
Luc The Perverse - 22 Oct 2005 13:18 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.