private void fpToDec() { if (calculator.getDisplayText().length() == 0) return; try { Integer displayNumber = Integer.valueOf(calculator.getDisplayText(), 16); Double converted = Utils.fpToDec(displayNumber); if ((displayNumber > 944.44444) || (displayNumber < -944.44444)) { throw (new IllegalArgumentException()); } calculator.setDisplayText(converted.toString()); } catch (NumberFormatException e) { calculator.setDisplayText("Formato de numero invalido"); calculator.setErrorMode(); } catch (IllegalArgumentException e) { calculator.setDisplayText("Numero fuera de rango"); calculator.setErrorMode(); } }
// Sirve para numeros entre -127 y 128 private void hexToDec() { if (calculator.getDisplayText().length() == 0) return; try { Long aux = Long.valueOf(calculator.getDisplayText(), 16); aux = (aux > 127) ? (long) (aux - 256) : aux; if ((aux > 127) || (aux < -128)) { throw (new IllegalArgumentException()); } calculator.setDisplayText(aux.toString()); } catch (NumberFormatException e) { calculator.setDisplayText("Formato de numero invalido"); calculator.setErrorMode(); } catch (IllegalArgumentException e) { calculator.setDisplayText("Numero fuera de rango"); calculator.setErrorMode(); } }
private void decToFp() { if (calculator.getDisplayText().length() == 0) return; String result = ""; try { Double displayNumber = Double.parseDouble(calculator.getDisplayText()); if ((displayNumber > 944.44444) || (displayNumber < -944.44444)) { throw (new IllegalArgumentException()); } result = Utils.hexString(Utils.decToFp(displayNumber)); } catch (NumberFormatException e) { result = "Formato de numero inválido"; calculator.setErrorMode(); } catch (IllegalArgumentException Exception) { result = "Número fuera de rango"; calculator.setErrorMode(); } catch (LostPrecisionException e) { result = Utils.hexString(e.getNumber()); calculator.showPrecisionLostDialog(); } finally { calculator.setDisplayText(result); } }
// Sirve para numeros entre -127 y 128 private void decToHex() { if (calculator.getDisplayText().length() == 0) return; try { Long displayNumber = Long.parseLong(calculator.getDisplayText()); Long number; number = Math.abs(displayNumber); if ((displayNumber > 127) || (displayNumber < -128)) { throw (new IllegalArgumentException()); } if (displayNumber < 0) { // Paso el numero a binario y completo con 0 hasta 8 bits String binary = Long.toBinaryString(number); while (binary.length() < 8) { binary = '0' + binary; } // Invertir bits char[] inverted = binary.toCharArray(); for (Integer i = 0; i < inverted.length; i++) { if (inverted[i] == '0') inverted[i] = '1'; else inverted[i] = '0'; } String invertedString = new String(inverted); number = Long.parseLong(invertedString, 2) + 1; } calculator.setDisplayText(Long.toHexString(number)); } catch (NumberFormatException e) { calculator.setDisplayText("Formato de numero invalido"); calculator.setErrorMode(); } catch (IllegalArgumentException Exception) { calculator.setDisplayText("Numero fuera de rango"); calculator.setErrorMode(); } }