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());
Related
This question already has answers here:
how to set value of octal in java?
(9 answers)
Closed 2 years ago.
public class Main
{
public static void main(String[] args) {
char a='3';
int b=011;
System.out.println(a+b);
}
}
Output is 60
can someone explain why java behaves like this ?
Literals with a leading zero are octal literals. Any number prefixed with a 0 is considered octal. Octal numbers can only use digits 0-7, just like decimal can use 0-9, and binary can use 0-1. To define integer literals as octal value in Java is effortless.
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
This question already has answers here:
In Java, is the result of the addition of two chars an int or a char?
(8 answers)
Closed 4 years ago.
IntelliJ IDEA Capture
Why i am getting 152, I think it will give me an error.
Please explain it.
public class character {
public static void main(String[] args) {
char myCharValue1 = 'A';
char myCharValue2 = '2';
char myCharValue3 = '%';
System.out.println(myCharValue1 + myCharValue2 + myCharValue3);
}
}
That is because chars refer to a number, which in turn has an ASCII representation.
Looking at an ASCII table you can see that the chars A, 2 and % have following values respectivly: 65, 50 and 37.
Adding those numbers together, you'll end up with 152 which is what you got in your example.
To print out those chars you could use following:
System.out.printf("%s%s%s&n", myCharValue1 + myCharValue2 + myCharValue3);
Which will print A2% (and a newline)
The concatenation + is for String. What you're doing is adding the numeric values of your chars and printing them together.
If you start with "" and then use + as Patrick Parker shows in his comment, it will become concatenation instead of simple addition and you'll get the result you expect.
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()
Yesterday, I attempted to do this one way...today I am trying another and I am still stuck. I have to find a way of doing this using integer division and mod. Here is my code followed by the error messages.
public int evaluateFraction(int w, int n, int d)
throws NumberFormatException
{
whole = w;
numerator = n;
denominator = d;
return portion2;
}
Tester
input = JOptionPane.showInputDialog("Enter");
portion2 = Integer.parseInt(input);`
error messages:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1 1/8"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at ClientCode.main(ClientCode.java:43)
Java Result: 1
What on earth am I doing wrong now?
Integer.parseInt is able to parse only valid integer strings. If the input string contains anything other than digits then it will throw NumberFormatException.
You are trying to parse an expression 1 1/8, which is not a valid integer string.
"1 1/8" is not a number. 1 and 8 are, whitespace and / are not. You need to parse such expression by hand.
1 1/8 is not an integer, Integer.parseInt can perform well in the only one case, if data is valid.
Don't know, what result you expect but you either need some other method or parse it yourself.