• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Converting byte[] to hex string

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm retrieving a 128-bit objectGUID from an LDAP (e.g. 7722D51DB86567...) as hex). I retrieve it as a byte array and need to convert it to a human readable hex string. I've seen plenty of methods that do this in various ways but I have two issues. First, I want the hex value in the range 0 to 255, not -128 to 127 (which it is stored in as bytes). And I also need to make sure that the hex value has a leading zero when less than 0x0F. Thanks.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try looking at the following article, which might clue you as to the approach you want.
JHexEdit
I hope this helps!
Regards,
Aaron R>
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this do what you need?
 
Wes Hughes
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its close but I think its just flipping the sign, so I'm only getting values in the range 0x00 to 0x80 still. I'm currently using this but it isnt pretty:

[ August 29, 2003: Message edited by: Wes Hughes ]
[ August 29, 2003: Message edited by: Wes Hughes ]
 
Gary Mann
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit confused. The code I posted should give the correct hex representation of whatever is in the byte array. If you run it and only get values between 0x00 and 0x80 then that is what is in the byte array. Could you explain a little more? Perhaps you could you give the binary values for a single byte and explain what you expect to see.
The code you posted has a few errors and looks like it won't compile so doesn't really help me understand.
 
Wes Hughes
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After a better second look, your code does just what I want. The only thing I changed was to use a StringBuffer to store the results.
 
Gary Mann
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it helps. You're right about the StringBuffer - definitely more efficient.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!