Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Convert Hexadecimal number into Integer

 
Mohammed Abdul Ali
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new in Java programming. I am trying to convert hexadecimal number into integer and getting NumberFormatException error. The code sample is:

String str1 = "0XA800001D";

String str2 = str1.replaceFirst("0X", "");

int value_int = Integer.parseInt( str2.trim(), 16 );

Can anybody please help me!!!




 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java integers are signed. And that hexidecimal number is too big to fit.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using a long as intermediate result:
This code will first create a long with value 2818572317, then the cast to int will turn it into -1476394979.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic