• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hex ??

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to take a byte array that has
E3 8C A1 D9 20 C4 B8 B8 D3 94 6B 2C 72 F0 16 80
in it.

And print out the string

I was thinking to use the constructor String( byte[] ). But that isn't really working.


It just prints -29-116-95-3932-60-72-72-45-10810744114-1622-128.
Which is just the integer representation of the hex values.

Basically I want to appeand the hex values as hex not as the integer equivalent.

I know I can use an array { E3 , -29 } so for the an do a look up.
But their should be an easy way to do it in java without having to create a lookup up table string or other things.

Is their a method that allows you to display in hex strings??

Thanks for any help
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait I found Integer.toHexString() .
I guess I can cast the byte to a int and then pass it thru the toHexString method for each byte in the byte array then append each value to one another.

Still wondering if their is another way to do it using the String class or something else.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sam Doder:
Wait I found Integer.toHexString() .
I guess I can cast the byte to a int and then pass it thru the toHexString method for each byte in the byte array then append each value to one another.

Still wondering if their is another way to do it using the String class or something else.



The only problem with Integer.toHexString() is that it doesn't put leading zeros in, so the values 0f 0 - 15 will come out as 0, 1, 2, 3, etc rather than 00, 01, 02, 03, etc.

You could use String.format to format each byte in turn and then append them together (it's probably better to use a StringBuilder to do the appending).
As a small optimisation, if your byte array is always the same length or is always a multiple of a given number long, you could format more than one byte with each call to String.format.
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, that is my only problem with toHexString()

4 prints as 4 and not 04 so I miss the zero when I append the values.

But I can fix this by check to see if the toHexString() function returns a string of length 1 or 2. And if it returned a string of length one append a zero before the string.

I was just wondering if their was a function Maybe in the packages java.security.* that you could get the md5 hash as a string instead of the byte array returned by digest() ;

Thanks for the reply.
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this should do it.
I don't see any place where it could fail.
Apparently I need to and it with 0xFF. Probably because of unsigned and signed data issues. But I would think and 0xFF with anything would just produce that anything (i.e md[i] & 0xFF = md[i] )
I don't know weather it is when you cast it to an integer maybe something is different. Or maybe it should be (int)md[i] & 0xFF instead but does the same thing as I can see.

-26 = E6 , or FFE6 or FFFFFFE6 etc etc depending on if we are talking about
a byte or word or dword or , etc etc...

so E6 & FF = E6 but FFE6 & FF = E6 Ahh so this is probably it?
An int in java is 32 bits so we are just talking the first byte of it.

I just need clarity am I correct on the reason why I need & 0xFF. Just because byte is the only signed number.


[ December 04, 2008: Message edited by: Martijn Verburg ]
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are the %f tags in the Formatter class.

Set up a StringBuilder.
Get each byte into hexadecimal format, possibly using the %x tag with 02 to get a leading 0 in (%02x).
Append each byte to the StringBuilder.
See what happens with negative bytes (0x80 to 0xff inclusive).
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ,but I figured out my method will always work.
Plus the fact StringBuilder is not in java 1.4.2 or below.
Which is not a big problem just would like it to work in all versions.
 
reply
    Bookmark Topic Watch Topic
  • New Topic