Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Byte to Hex String

 
Scott Selikoff
author
Saloon Keeper
Posts: 4015
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to convert a Java byte to a 2-digit Hex string hex string. I can use the System output to get what I want, but I'm having trouble writing a general method.

In short, I'm looking for the equivalent of doing the following:



..but with the ability to append the results to a StringBuilder/StringBuffer object. Any suggestions? I searched online a bit but the examples I came across included manually converting byte values which seemed a bit excessive. I'm assuming there's something in the API to simplify this in a one-line call.
 
Paul Clapham
Sheriff
Posts: 21149
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about Integer.toString(byte, 16) for a start? You'd have to tack on the leading zero for numbers below 16, though.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.format("%02X", myByte)

Would probably work. Maybe Integer.toHexString(myByte) though I am not sure that does the leading zero...
 
Scott Selikoff
author
Saloon Keeper
Posts: 4015
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:String.format("%02X", myByte)

Would probably work. Maybe Integer.toHexString(myByte) though I am not sure that does the leading zero...


Thanks Steve, that does work. I knew there was something added in 1.5 to facilitate this but I couldn't remember. Since I need the leading zero, I'll use String.format().
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is very very easy. Class java.util.Formatter has an constructor that allows one to specify an Appendable. You can then us the format() method to write values into that Appendable. Both StringBuidler and StringBuffer implement Appendable.

Take a look at my last post in this thread.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, James. Using a Formatter by passing a StringBuilder to it is efficient, because the Formatter directly writes to the StringBuilder, so strings don't have to be copied, which is useful especially if you have to format for example a byte[] to hex characters. I needed this today, here's what I wrote:
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like returning a String unless I need a String so if I were doing this I would probably overload the method along the lines of -


As an aside - I'm never sure whether it is more efficient to index an array of primitives or iterate over it. Since the second parameter to the format() method is a varags Object it probably makes no difference her since an Integer or Byte wrapper will be created anyway. I suspect that the cost of having to parse the "%02x" greatly outweighs any cost of creating the wrappers.
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way is to do all the hard work yourself:
 
Scott Selikoff
author
Saloon Keeper
Posts: 4015
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone, but I'll stick with the one line solution. No reason to reinvent the wheel!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just want to format one byte into a pair of hex digits, then String.format("%02X", b); is ofcourse the only thing you need. But our last few posts were about what if you have an array of bytes that you want to convert to a string of hex characters.
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For short term use, String.format should definitely be used. James' and my solutions are minor optimizations that will only make sense if called a lot and/or if you have limited resources. James' solution creates a new Formatter each time, mine doesn't. That's the only difference between the two.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure Rob? Look at line 3 of your last post above I guess you left that in there accidentally.
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhm, yes. Guess that's a copy-paste error Unfortunately, the forum doesn't warn about unused variables like Eclipse does
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic