• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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: 13081
67
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: 76811
366
  • 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.
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic