NumberFormatException from hex number [duplicate] - java

This question already has answers here:
Convert hex string to int
(9 answers)
Closed 8 years ago.
In my code I have
int i = Integer.parseInt("f8004896",16);
when I run the program it throws a NumberFormatException
java.lang.NumberFormatException: For input string: "f8004896"
What am I doing wrong? This seems relatively straightforward but its not working.

long i = Long.parseLong("f8004896", 16);
System.out.println(i);
System.out.println(Integer.MAX_VALUE);
Output:
4160768150
2147483647

Decimal value for f8004896 is 4160768150 and it's more than 2^31-1 (upper limit for int type). So you should use long instead: Long.parseLong()

Related

Why my function is displaying a weird result when converting string to double? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I have a very weird result when I'm using my function and I think that I'm missing something with the rounding and double in java.
For example, when I provide the value 00159,300 for number and 100 for conversion I have 15930,000000000002 which is not possible!
public static String convertMultiply(String number, String conversion) {
number=number.replace(",", ".");
BigDecimal res=BigDecimal.valueOf(Double.valueOf(number)*Integer.valueOf(conversion));
res=res.stripTrailingZeros();
return res.toPlainString().replace(".", ",");
}
thanks in advance!
Double is an approximation of decimal values in Java. Instead, replace your line using double with:
BigDecimal res = (new BigDecimal(number)).multiply(new BigDecimal(conversion));

how to convert OX12 to decimal [duplicate]

This question already has answers here:
Convert hex string to int
(9 answers)
Closed 2 years ago.
I need to convert hexadecimal value which is OX12 to decimal in JAVA,
as I knew hexadecimal is based 16 (1..9, a..f), but in this case, I don't know how to convert with that value.
Can someone help me?
Thanks so much
If the number is a String, you can use the parseInt method of the Integer class with first argument "12" (the number in hex) and second argument 16, the radix, or base, of the number
int number = Integer.parseInt("12", 16);
System.out.println(number);
// output is 18
If the number is not a String, you can simply do the following:
int number = 0x12;
System.out.println(number);
// output is 18

How to take 2 digits number in string and convert it to int [duplicate]

This question already has answers here:
Splitting and converting String to int
(5 answers)
Closed 3 years ago.
String code = "U 12 24";
int s = Integer.parseInt(String.valueOf(code.charAt(2)));
System.out.println(s);
that would be print 1,
however, i want to try print 12 or i mean i want take 2 digits number, but i can't do it because the only way i know is just take one digit number.
how if i want take 12 and convert to int
int s = Integer.parseInt(String.valueOf(code.substring(2, 4)));
If you want to get all digits in a given string, you have to tokenize the string by space and parse every chunk into a number.

Java - parse binary to long [duplicate]

This question already has an answer here:
Java 8: Why can't I parse this binary string into a long?
(1 answer)
Closed 6 years ago.
I have a binary represenation of a number and want to convert it to long (I have Java 8)
public class TestLongs {
public static void main(String[] args){
String a = Long.toBinaryString(Long.parseLong("-1")); // 1111111111111111111111111111111111111111111111111111111111111111
System.out.println(a);
System.out.println(Long.parseLong(a, 2));// ??? but Long.parseUnsignedLong(a, 2) works
}
}
This code results in Exception in thread "main" java.lang.NumberFormatException: For input string: "1111111111111111111111111111111111111111111111111111111111111111"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
1111111111111111111111111111111111111111111111111111111111111111
at java.lang.Long.parseLong(Long.java:592)
What is wrong here? Why Long.parseLong(a, 2) doesn't work?
Long.parseLong() doesn't treat the first '1' character as a sign bit, so the number is parsed as 2^64-1, which is too large for long. Long.parseLong() expects input Strings that represent negative numbers to start with '-'.
In order for Long.parseLong(str,2) To return -1, you should pass to it a String that start with '-' and ends with the binary representation of 1 - i.e. Long.parseLong("-1",2).
Eran is right, and the answer to your question is:
System.out.println(new BigInteger(a, 2).longValue());

Convert ascii to int? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert ascii to int ??
int a=53
This is an ASCII value for 5 i want Convert it to Integer
Are you looking for something like this?
int asciiValue = 53;
int numericValue = Character.getNumericValue(asciiValue);
System.out.println(numericValue);

Categories