Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

NumberFormatException when using Integer.decode with hex numbers

807580Apr 27 2010 — edited Apr 27 2010
hi folks, I have a simple test code to convert a hex string to an Integer. The odd thing is that it works for my hex strings that contains only numbers 0-9 but fails with a NumberFormatException for any hex string with the LEGAL hex characters a-f or A-F. The two examples below show the issue,

import java.io.*;
import java.util.*;

public class decodeTest {
	public static void main(String[] args) throws NumberFormatException {

		//String t = "0xdeadbeef";
		String t = "0x12345678";

		Integer tmp = Integer.decode(t);
		System.out.println("String t = "+t);
		System.out.println("Integer tmp = "+tmp);

	}
}
The result is as expected ,
java decodeTest
String t = 0x12345678
Integer tmp = 305419896


If I run the code (just replacing the hex string with any number of LEGAL hex characters a-f or A-F I get,
import java.io.*;
import java.util.*;

public class decodeTest {
	public static void main(String[] args) throws NumberFormatException {

		String t = "0xdeadbeef";
		//String t = "0x12345678";

		Integer tmp = Integer.decode(t);
		System.out.println("String t = "+t);
		System.out.println("Integer tmp = "+tmp);

	}
}
java decodeTest
Exception in thread "main" java.lang.NumberFormatException: For input string: "deadbeef"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:484)
at java.lang.Integer.valueOf(Integer.java:543)
at java.lang.Integer.decode(Integer.java:941)
at decodeTest.main(decodeTest.java:10)


Any ideas would be greatly appreciated!!! (This error repeats for any hexstring with even one character of a-f or A-F)

Mike
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 25 2010
Added on Apr 27 2010
5 comments
3,808 views