Why does this hexadecimal value gets different decimal value? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to dynamically set an integer variable using a hexadecimal value, but when I use Integer.parse(hexValue, 16) it gets a different value from setting as int a = 0x04A7D488
For example:
int a = 0x04A7D3B8;
System.out.println("a = " + a); // prints 78107576
int b = Integer.parseInt("04A7D3B8", 16);
System.out.println("b = " + b); // prints 78107784
Why do I get different values? How can I dynamically set variable a with value 0x04A7D3B8?
Note: I've discovered that this error is only happening with Java SDK 1.8.0_171.

Solution
I was trying to convert a negative integer value using Long.parseLong or Integer.parseInt, but the correct solution is using Integer.parseUnsignedInt("FD8914EC");
In my tests, the value FD8914EC was converting to -41347860 (declaring as long a = 0xFD8914EC) or 4253619436 (declaring as long b = Long.parseLong("FD8914EC", 16);), but you have always to use Integer.parseUnsignedInt (the result will be negative if the hexadecimal value starts with F).

Related

Integer.parseInt giving java.lang.NumberFormatException in Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a character array as
char[] bitsString = new char[16];
bitsString = {'1','1','1','1','1','1','0','1','1','1','1','0','0','1','1','0'};
Then I converted it to the corresponding integer as follows:
int givenNumber = Integer.parseInt(new String(bitsString), 2);
The above logic works fine when number bitstring array length is less than 10. But when it is increased to 11 or more, it is showing me java.lang.NumberFormatException why ?
After some hit and trials I found that, it was the case since the 16 bit value is crossing the Integer limit.
But when it is changed from int to long
long givenNumber = Long.parseLong(new String(bitsString), 2);
it is working perfectly fine. Since in this case long has 64 bits length.

Rounded result even casting to float [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I don't know why java is rounding result. I used casting to float, I was adding '.0f'. Nothing want to work. I know that double is better for dividing but I don't need very precision result.
int A = Integer.parseInt(listBytesAnsw.get(2), 16); //ex. 18
int B = Integer.parseInt(listBytesAnsw.get(3), 16); //ex. 226
float rpm = (float) (A*255+B)/4; //Ans=1204 wrong, should be 1203.75
float rpm = (float) (A*255.0f+B)/4.0f; //dont work still 1204
The result you get is to be expected. After all:
255*226 = 4816
4816 / 4 = 1204
Of course, rounding and then casting would not work in case you have indeed a non-integer result. So look at the following code
System.out.println("(float)(7/4)=" + ((float)(7/4)));
System.out.println("(float)7/4=" + ((float)7/4));
System.out.println("(7+0.0f)/4=" + ((7+0.0f)/4));
System.out.println("7/(4+0.0f)=" + (7/(4+0.0f)));
results in
(float)(7/4)=1.0
(float)7/4=1.75
(7+0.0f)/4=1.75
7/(4+0.0f)=1.75
The first does not work as you found out. But either of the other solutions works.

I am getting a "not a statement" error on a for each loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The problem seems to be in the increment, but I need it to decrease by 2. The "length" variable is for the length of a series of numbers
public int longMethodName()
{
int length = cardNumber.length();
longMethodName = 0
for(int i=length-1; i<0; i-2)
{
int cardNumberInt = Integer.parseInt(cardNumber.charAt(i));
int tempVar = cardNumberInt*2;
longMethodName = longMethodName + tempVar;
}
return longMethodName;
}
You need to change it to i=i-2 or i-=2 to decrement by 2.
You might be trying to emulate the i++/i-- syntax, which is simply shorthand for i = i+1 or i=i-1. However, that syntax only works for change by 1 (formally speaking ++ and -- are unary operators ), so i-2 won't work directly.
You also need to fix the other errors, as detailed in the other answer.
1.) longMethodName = 0 // Semicolon missing
2.) i-2, need to change to i = i-2
3.) Integer.parseInt(), cardNumber.charAt(i)returns char which is not allowed
You can also use i-=2 so you don't have to write i a second time ;-)

How to subtract a number(int) from a character in java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I'm trying to subtract an int from a char but I keep being told that the compiler "cannot convert from int to char".
I have tried changing the constant to a char but it didn't help.
is there any easy way to do this subtraction?
test[1] = characterArray[1] - ASCII_SUB;
Any help much would be appreciated.
The problem is that subtraction is never performed on char values in Java. Instead, both operands are promoted to int (via binary numeric promotion), and the result of the subtraction is an int as well. So you'll need to cast the result back to char:
test[1] = (char) (characterArray[1] - ASCII_SUB);

Cannot convert valid string to long [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have String which cannot be converted to corresponding Long number using Long.getLong(id). The value of string seems to be valid. Why returned longId is null?
Code:
Long longId = Long.getLong(id);
Use Long.valueOf(..). Long.getLong(...) is for different purpose:
getLong(String nm)
Determines the long value of the system property with the specified name.
Use Long.parseLong(id) to convert from a String to a Long.
This has already been answered on StackOverflow. Please check the following URL:
How to convert String to long in Java?
Credit to Mike Christensen and Josh Pinter for the original answer.
Long.parseLong("0", 10) // returns 0L
Long.parseLong("473", 10) // returns 473L
Long.parseLong("-0", 10) // returns 0L
Long.parseLong("-FF", 16) // returns -255L
Long.parseLong("1100110", 2) // returns 102L
Long.parseLong("99", 8) // throws a NumberFormatException
Long.parseLong("Hazelnut", 10) // throws a NumberFormatException
Long.parseLong("Hazelnut", 36) // returns 1356099454469L
This will do it: Long.parseLong("100")

Categories

Resources