• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hexadecimal String to int

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to convert a hexadecimal String to an int. If I use for examlpe

it would be valid code, but if I do

It will result in an error;
Exception in thread "main" java.lang.NumberFormatException: For input string: "0xff19aed8"
why does this happen and how do I fix this?
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Luc,

first of all: 'x' is not a legal character in the base-16 system. Second, your string, as int, does not fit into an int. The maximum value that you can use is "7FFFFFFF". If you do want your string to parse, use Integer.parseUnsignedInt.
 
Saloon Keeper
Posts: 7582
176
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "0x" is just for the literal integer; you need to leave it out when representing the number as  string.

It looks as if the number might be too large for an integer, in which case you'd need to use Long.parseLong instead.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:. . . use Long.parseLong instead.

But 0xff19aed8 is negative; if you parse it as a long, it will come out positive. What happens with Integer.valueOf("ff19aed8", 0x10).intValue()? Can you use any of the unsignedXXX() methods introduced in Java8? Is this method any use?

jshell> Integer.parseUnsignedInt("ff19aed8", 0x10);
$2 ==> -15094056

jshell> System.out.printf("%#010x%n", Integer.parseUnsignedInt("ff19aed8", 0x10));
0xff19aed8
$6 ==> java.io.PrintStream@5a10411

Yes, it would appear, “That's the way to do it.”
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends on the requirements.

Both Integer.parseUnsignedInt(String, int) and Long.parseLong(String, int) are valid, depending on whether you want to interpret the input string as an unsigned 4 byte value or a signed 8 byte value.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Depends on the requirements. . .  

Agree.

Long.parseLong(String, int). . . .

What will happen if you cast your long to an int? In this instance, nothing:-

jshell> System.out.printf("%#010x%n", (int)Long.parseUnsignedLong("ff19aed8", 0x10));
0xff19aed8
$7 ==> java.io.PrintStream@5a10411

jshell> System.out.printf("%#010x%n", (int)Long.parseLong("ff19aed8", 0x10));
0xff19aed8
$8 ==> java.io.PrintStream@5a10411

jshell> System.out.printf("%#010x%n", Long.parseUnsignedLong("ff19aed8", 0x10));
0xff19aed8
$9 ==> java.io.PrintStream@5a10411

jshell> System.out.printf("%#010x%n", Long.parseLong("ff19aed8", 0x10));
0xff19aed8
$10 ==> java.io.PrintStream@5a10411

 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only because the 'x' conversion treats integers as unsigned.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the %x conversion make any difference? It shows that all the outputs are the same, whether 0xff19aed8 or −15094056.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. If you use the 'd' conversion you'll see that it interprets its arguments as signed integers. In the case of int it will print a negative value and in the case of long it will print a positive value.

The cases where you cast the parsed long to int will print "-15094056" while the cases where you don't cast to int will print "4279873240".
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn! I missed that case.
 
Yes, my master! Here is the tiny ad you asked for:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic