Home > Back-end >  hexadecimal calculator issue with subtraction and division operation
hexadecimal calculator issue with subtraction and division operation

Time:12-12

I have an app Base calculator and I'm facing an issue with subtraction between two base hexadecimal. if a (big number subtraction a small number) it gives the correct result. the problem is when the operation ( small number subtraction big number ) for Example: (1 - 22 or 1a - 22 ) the app crashes and closes.

The hexadecimal method is :

private void hexa_calc() {

    int num = 0;

    int num1 = Integer.parseInt(e_one.getText().toString().trim(),16);
    int num2 =Integer.parseInt(e_two.getText().toString().trim(),16);

    if (appKey1.operation.equals(" ")) {
        String value = Integer.toHexString(num1   num2);

        num = Integer.parseInt(value, 16);
        t_result.setText(String.valueOf(Integer.toHexString(num)));

    } else if (appKey1.operation.equals("-")) {

        String value = Integer.toHexString(num1 - num2);

        num = Integer.parseInt(value, 16);
        t_result.setText(String.valueOf(Integer.toHexString(num)));

    } else if (appKey1.operation.equals("X")) {

        String value = Integer.toHexString(num1 * num2);
        num = Integer.parseInt(value, 16);

        t_result.setText(String.valueOf(Integer.toHexString(num)));

    } else {


        String value = Integer.toHexString(num1 / num2);
        num = Integer.parseInt(value, 16);

        t_result.setText(String.valueOf(Integer.toHexString(num)));
    }
}

the second issue with the division operation is it gives the (result = 0 ) it's not like this 0.12324 without decimal after zero.

how can I solve this code?

CodePudding user response:

  1. When you transform a negative integer to a hex string you get a hex representation of your number plus 2^32. For example, Integer.toHexString(-3) gives you fffffffd. You should have directly set your result field to this value, but instead you are trying to convert this string back to an integer number and again to the hex string. This additional and unnecessary pair of transformations fails. It fails because hex number fffffffd is too big to be represented by an integer number. It can only fit in Long. So,
Integer.parseInt(Integer.toHexString(-3), 16);

fails with java.lang.NumberFormatException, but

Long.parseLong(Integer.toHexString(-3), 16);

gives you 4294967293, which is 2^32 - 3.

TLDR, you should avoid the transformation of the result to integer and back to hex string. Simplify your code to

String value = Integer.toHexString(num1 - num2);
t_result.setText(value);

The same is true for other operations, not just for the subtraction.

  1. With the division the problem is that the type of the result is integer. Of course, the integer result cannot have any fractional part. You should use double to hold the result. For example:
String value = Double.toHexString((double)num1 / num2);
t_result.setText(value);

This will give you the fractional part.

  • Related