Programación General > Java
Decimal a Hexadecimal, Código
(1/1)
adn7negro:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package guilasconversiones;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 281176
* @author Ezequiel
*/
public class DecimalHexal {
public static void main(String[] args) {
int numero =0;
int dividendo =0;
int residuo =0;
int cociente =0;
int [] hexal = new int [16];
int tamaño = 0;
int otro=0;
//Capturamos el numero
BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Introduzca el numero decimal: ");
try {
numero= Integer.parseInt(entrada.readLine());
}catch (IOException ex) {System.out.println("Numero Invalido ");
}catch (NumberFormatException ex) {System.out.println("Numero Invalido ");
}
//Guardamos el numero ingresado en pantalla en la variable dividendo
dividendo =numero;
if(dividendo==0){
otro=dividendo;
}
//Realizar operaciones y almacenar en vector con tamaño definido por contador
while (dividendo > 0){
cociente = dividendo / 16;
residuo = dividendo % 16;
hexal [tamaño] = residuo;
tamaño = tamaño + 1;
dividendo = cociente;
}
//Mostrar el resultado en pantalla
System.out.println("El numero Hexal es: ");
for (int i =(tamaño-1);i>=0;i--){
if (hexal >= 0 && hexal <= 9){
System.out.print(+hexal);
}
else
if (hexal ==10)
System.out.print("A");
else
if (hexal ==11)
System.out.print("B");
else
if (hexal ==12)
System.out.print("C");
else
if (hexal ==13)
System.out.print("D");
else
if (hexal ==14)
System.out.print("E");
else
if (hexal ==15)
System.out.print("F");
System.out.println(" ");
}
}
}
posman:
Como ejercicio esta bien. Pero no es mejor utilizar la funcion toHexString ?
--- Código: Text ---import java.io.*; class Main { public static void main(String[] params) { Console c = System.console(); if ( c == null ) { System.err.println("No console"); System.exit(1); } String cadena, num; while ( !(cadena=c.readLine()).equals("") ) { num = Integer.[b][color=#0000FF]toHexString[/color][/b](Integer.parseInt(cadena)); System.out.println("0x" + num.toUpperCase()); } }}
adn7negro:
Impresionante Maestro... Lo tomaré en cuenta en un futuro cercano!!!
Si, la idea es practicar razonamiento lógico de nivel principiante
Gracias!!!
Navegación
Ir a la versión completa