• 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

Java Byte to Hex String

 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way is to do all the hard work yourself:
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic