1

I am receiving an exception when I insert to many hex characters into a string that is being converted to an integer value.

Code:

//variables used in this problem
int INT_MAX = 2147483647;
int INT_MIN = -2147483648;
int currentBase = 16;
string displayValue;

switch(currentBase) {
    case 16:
        if((Integer.valueOf(displayValue + "F", currentBase) >= INT_MIN) && (Integer.valueOf(displayValue + "F", currentBase) <= INT_MAX))
        {
            displayValue += "F";
        }
        break;
}

The error message:

E/AndroidRuntime(6944): Caused by: java.lang.NumberFormatException: Invalid int: "FFFFFFFF"
E/AndroidRuntime(6944):     at java.lang.Integer.invalidInt(Integer.java:138)
E/AndroidRuntime(6944):     at java.lang.Integer.parse(Integer.java:378)
E/AndroidRuntime(6944):     at java.lang.Integer.parseInt(Integer.java:366)
E/AndroidRuntime(6944):     at java.lang.Integer.valueOf(Integer.java:510)
E/AndroidRuntime(6944):     at com.example.calculator.MainActivity.send_f(MainActivity.java:968)

I need this string to have a value no larger than size int (32 bits). Any suggestions?

Taylor
  • 17
  • 9
  • 2
    Make your number smaller? What are you asking? – SLaks Sep 01 '13 at 03:10
  • the number is user input, i am trying to use this value in a calculator that supports doublewords (32 bits). – Taylor Sep 01 '13 at 03:13
  • As you seem to already be aware, Java `int` values range from -2147483648 to 2147483647. The hex value 0xFFFFFFFF is 4294967295, which is outside this range. – Greg Hewgill Sep 01 '13 at 03:15
  • how could i take 0xFFFFFFFF and use it to represent its negative value (-1)? – Taylor Sep 01 '13 at 03:18

4 Answers4

1

Greg Hewgill's comment is correct. It looks like you're trying to have the bits be automatically interpreted. Probably the simplest thing to do is something like

 Long.valueOf(displayValue + "F", currentBase).intValue();

to get the bits, and then truncate them to int. Of course, now it's your responsibility to ensure that the argument to Long.valueOf fits in 32 bits.

Stephen Niedzielski
  • 2,497
  • 1
  • 28
  • 34
Jeremy
  • 326
  • 1
  • 5
  • thank you! i got everything working and now have a much better understanding of how these conversions work! – Taylor Sep 01 '13 at 04:19
1

FFFFFFFF is too big, max int value is 0x7FFFFFFF the workaround is

int i = (int)Long.parseLong(hexStr, 16);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0
try {
    // try your calculation here
} catch (NumberFormatException e) {
    // assign the variable the max/min value instead
    // or whatever behavior you want
}

This code will provide default behaviors for numbers that are out of range.

user1549672
  • 486
  • 1
  • 6
  • 16
0

It seems to me you are using a wrong method for hex string to int conversion , if my this understand is wrong let me know

I am quoting a duplicate post

    String hex = "ff"

// small values
    int value = Integer.parseInt(hex, 16);  

// big values
    BigInteger value = new BigInteger(hex, 16);

refer for the duplicate post i quoted

API refrence for the method

Community
  • 1
  • 1
Srinath Ganesh
  • 2,496
  • 2
  • 30
  • 60