aspose file tools*
The moose likes Java in General and the fly likes Convert byte to its hex format? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert byte to its hex format?" Watch "Convert byte to its hex format?" New topic
Author

Convert byte to its hex format?

Stephanie, Smith
Greenhorn

Joined: May 22, 2001
Posts: 12
I have a byte (that is actually an integer in its byte form).
How can I convert this byte to its hexFormat in a String?
Indu, B
Greenhorn

Joined: Jun 27, 2001
Posts: 1
You can try using
public static byte parseByte(String s,
int radix)
throws NumberFormatException
where radix is 16
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Indu, B,
JavaRanch has a Naming Policy Please reregister with an appropriate user name. It can benefit you, see Book GiveAway.
Stephanie,
You'd use
byte b = 7;
String hex = Integer.toHexString(b);
------------------
Hope This Helps
Carl Trusiak, SCJP2


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Stephanie, Smith
Greenhorn

Joined: May 22, 2001
Posts: 12
The integer 240 is returned as an array of bytes.
I tried
for (int i =0; i < ipBytes.length; i++)
{
byte b = ipBytes[i];
String hex = Integer.toHexString(b);
System.out.println("Byte as hex string is: " + hex);
}

and I get
Byte as hex string is: a
Byte as hex string is: 1a
Byte as hex string is: b
Byte as hex string is: fffffff0
WHy is this last byte fffffff0?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think the real question here is, where did this array of bytes come from? It looks like the first three bytes are simply wrong, and the fourth suffers from conversion problems. The correct value of 240 in hex is given by Integer.toHexString(240), which gives "F0". The problem is that a byte in Java is assumed to have a range from -128 to +127, so when you put 240 = F0 into a single byte, and then try to do other operations with that number, Java assumes that the F0 represents a negative value (-16 in this case). And when you use Integer.toHexString(), which expects an integer argument, Java converts the byte to int using sign extension. This means that since F0 = 11110000 represents a negative value (as far as Java is concerned), a bunch of 1's will be added to the left hand side of the number until you get 11111111 11111111 11111111 11110000 = FFF0 (representing -16 as an int), which is what was printed.
If you really need to know the hex string for a single byte without sign extension (i.e. assuming it represents a value between 0 and 255 rather than -128 to 127), use the following:
<code><pre>
byte b = (byte) 240;
System.out.println(Integer.toHexString(0x000F & b));
</pre></code>
By performing a bitwise AND with 0x000F, we effectively set the first 12 bits of the result to zero, meaning that any sign extension is cancelled out.


"I'm not back." - Bill Harding, Twister
Stephanie, Smith
Greenhorn

Joined: May 22, 2001
Posts: 12
Jim,
Thanks for that info, that explains why I'm seeing what I"m seeing. YOu can ignore the first 3 bytes I print out, they
are other numerics, not the 240. Just the last byte
was the 240 ..and your posting explained why I am seeing
what I"m seeing. I was just expecting to see "F0" and
was surprised when I didn't see that.
Thanks for the info.
Ulrich Winter
Greenhorn

Joined: Feb 19, 2002
Posts: 6
Ok, now I figured it out:
The simplest way is:
 
wood burning stoves
 
subject: Convert byte to its hex format?