Convert from hexadecimal to decimal in Java [closed]

0

I want to convert from hexadecimal to decimal. I'm developing in Android Studio, and here is the fragment of the method.

public void calcularDec() {

    EditText numero1 = (EditText) findViewById(R.id.etNumero1);
    int n = Integer.parseInt(numero1.getText().toString().trim(),16);


    Toast.makeText(this, "El resultado es: " + n, Toast.LENGTH_LONG).show();

}
    
asked by orlandb 23.05.2017 в 06:24
source

1 answer

0

You do not specify which is the input hexadecimal, so I leave you some examples explained: Int is able to store 2147483657 (0x7fffffff) so the hexadec value can not exceed that value, to solve that you can use Long:

EditText numero1 = (EditText) findViewById(R.id.etNumero1);
long n = Long.parseLong(numero1.getText().toString(), 16);
Toast.makeText(this, "El resultado es: " + n, Toast.LENGTH_LONG).show();

If your entry value is lower than previously mentioned, it is convenient to use Int.

EditText numero1 = (EditText) findViewById(R.id.etNumero1);
int n = Integer.parseInt(numero1.getText().toString(), 16);
Toast.makeText(this, "El resultado es: " + n, Toast.LENGTH_LONG).show();

Another way could be with this scheme

Integer.decode("0x4d2") //devolverá 1234
    
answered by 23.05.2017 / 07:28
source