Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hex Decode for Byte's  RSS feed

 
Shawn Smith
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hopefully this is simple for someone (forgive a poor c++ schmuck)

I have:

String hexVal = "0xFF";

which should be 1111 1111

I am trying to do a:

Byte b = Byte.decode(hexVal);

but I get a java.lang.NumberFormatException: Value out of range. Value:"FF" Radix:16

I've googled with no noticeable success, so any help will be greatly appreciated.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a WAG, but the api says "This sequence of characters must represent a positive value". The first F would make it negative, i believe.

Try -0xEF, maybe?
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have a hexadecimal unsigned literal . . . but you can't parse a number outwith the range of an integer. The String "0xFF" is different; it is interpreted as > 127. An awkward thing, I am afraid. The one suggestion I can make is to parse it to a wider typeI think other people will have better ideas.

And welcome to the Ranch
 
Shawn Smith
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, I figured it out shortly after the post. I can simulate 0xFF by using -0x01 (two's complement thank you very much), but that's not terriby intuitive, so I widened it out to a short and now it not only works, I have room to grow.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!