• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Hex Decode for Byte's

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13013
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 74342
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic