Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

hexadecimal String to int  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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?
 
Saloon Keeper
Posts: 3250
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 5475
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 64471
225
  • Mark post as helpful
  • send pies
  • 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: 10206
216
  • Mark post as helpful
  • send pies
  • 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: 64471
225
  • Mark post as helpful
  • send pies
  • 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: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only because the 'x' conversion treats integers as unsigned.
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • 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: 10206
216
  • Mark post as helpful
  • send pies
  • 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: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! I missed that case.
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!