aspose file tools*
The moose likes Java in General and the fly likes Java Byte to Hex String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Byte to Hex String" Watch "Java Byte to Hex String" New topic
Author

Java Byte to Hex String

Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

String.format("%02X", myByte)

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


Steve
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

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

Joined: Sep 07, 2004
Posts: 781

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.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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

Joined: Oct 27, 2005
Posts: 19696
    
  20

Another way is to do all the hard work yourself:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Thanks everyone, but I'll stick with the one line solution. No reason to reinvent the wheel!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Aug 16, 2005
Posts: 14150
    
  18

Are you sure Rob? Look at line 3 of your last post above I guess you left that in there accidentally.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Uhm, yes. Guess that's a copy-paste error Unfortunately, the forum doesn't warn about unused variables like Eclipse does
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Byte to Hex String