• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert byte to its hex format?

 
Stephanie, Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try using
public static byte parseByte(String s,
int radix)
throws NumberFormatException
where radix is 16
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephanie, Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephanie, Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now I figured it out:
The simplest way is:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic