Get your CodeRanch badge!*
The moose likes Java in General and the fly likes Java Convert Hexadecimal number into Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Convert Hexadecimal number into Integer" Watch "Java Convert Hexadecimal number into Integer" New topic
Author

Java Convert Hexadecimal number into Integer

Mohammed Abdul Ali
Greenhorn

Joined: Apr 24, 2010
Posts: 1
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
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39


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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Convert Hexadecimal number into Integer
 
Similar Threads
comparing Strings
Referencing to another object
Problem on String
Huffman encoding/Decoding
String Problem