Pregunta:
¿convertir de hexadecimal a decimal en java sin librerias?
anonymous
2011-08-24 19:03:36 UTC
necesito una función que me reciba un numero hexadecimal (en un String) y este lo convierta en un numero decimal , el problema es que no puedo usar librerias tales como Integer.toHexString y por el estilo
Tres respuestas:
anonymous
2011-08-24 20:06:05 UTC
Facil, simplemente descompones la cadena y si es una letra comparala con ABCDEF, SI es una letra, asignale su respectivo valor y multiplicalo por 16 elevado a la potencia de la posicion donde se encuentre, por ejemplo 2A recorres la cadena de atras para adelante o volteala con el metodo reverse() de la clase StringBuilder, y te quedaria A2



A = 10 X 16 ^ 0 = 10



2 X 16 ^ 1 = 32



ENTONCES 32+10 = 42



AQUI EL CODIGO :





import java.util.Scanner;



public class Hexadecimal {



public static long Hex_Decimal(String n)

{

StringBuilder conversion = new StringBuilder(n).reverse();

long suma=0;

for( int i = 0; i < conversion.length(); i++)

{

if(conversion.charAt(i)=='A')

suma = suma + 10 * (long)Math.pow(16, i);

else if(conversion.charAt(i)=='B')

suma = suma + 11 * (long)Math.pow(16, i);

else if(conversion.charAt(i)=='C')

suma = suma + 12 * (long)Math.pow(16, i);

else if(conversion.charAt(i)=='D')

suma = suma + 13 * (long)Math.pow(16, i);

else if(conversion.charAt(i)=='E')

suma = suma + 14 * (long)Math.pow(16, i);

else if(conversion.charAt(i)=='F')

suma = suma + 15 * (long)Math.pow(16, i);

else

{

suma = suma + Integer. parseInt("" + conversion. charAt(i)) * (long) Math. pow(16, i) ;

}

}

return suma;

}

public static void main(String[]args)

{

Scanner lee = new Scanner(System.in);

System.out.println("Ingresa un numero hexadecimal");

String numero = lee.nextLine().toUpperCase();

System.out.println( Hexadecimal. Hex_Decimal(numero) ) ;

}

}
Jhener
2011-08-28 00:14:58 UTC
//este seria el metodo que necesitas....y las variables tienen que ser globales...

//Primero hay que importar la libreria import java.text.*;



String texto = "", textoTemp = "";

double num = 0;



DecimalFormat df = new DecimalFormat("#########.00");



public String hexa_decimal(){

if(texto.equals("")){

return "0";

}else{

int j = 1;

if(texto.equals("")){return "0";}

else{

textoTemp = texto.substring(texto.length()-1);

if(textoTemp.equals("A")){textoTemp = "10";}

if(textoTemp.equals("B")){textoTemp = "11";}

if(textoTemp.equals("C")){textoTemp = "12";}

if(textoTemp.equals("D")){textoTemp = "13";}

if(textoTemp.equals("E")){textoTemp = "14";}

if(textoTemp.equals("F")){textoTemp = "15";}

num += Double.parseDouble(textoTemp) * Math.pow(16,0);

}

for(int i=texto.length()-2;i>=0;i--){

textoTemp = texto.substring(i,i+1);

if(textoTemp.equals("A")){textoTemp = "10";}

if(textoTemp.equals("B")){textoTemp = "11";}

if(textoTemp.equals("C")){textoTemp = "12";}

if(textoTemp.equals("D")){textoTemp = "13";}

if(textoTemp.equals("E")){textoTemp = "14";}

if(textoTemp.equals("F")){textoTemp = "15";}

num += Double.parseDouble(textoTemp) * Math.pow(16,j);

j++;

}

texto = df.format(num);

texto = texto.substring(0,texto.length()-2);

texto = texto.replace('.', ' ');

texto = texto.trim();

num = 0;

return texto;

}

}
Gral. Emiliano Zapata
2011-08-25 03:48:42 UTC
public class Test {

public static int toInt(String hex)

{



char [] hexstrings = "0123456789ABCDEF".toCharArray();

char [] values = new char[hex.length()];

String hex2 = hex.toUpperCase();



for (int i = 0; i < values.length; i++) {

values[i]=hex2.charAt(

values.length-1-i);

}

System.out.println(values);

int value = 0;

for (int i = 0; i
{

for(int j=0;j
{

if(values[i] == hexstrings[j])

{

int potencia = 1;

for(int k=0;k
value+=(potencia*j);

break;

}

}

}

return value;

}



public static void main(String[] args) {



int value = Test.toInt("145f");

System.out.println(value);

// System.out.println("El valor es"+0x145f);

}

}


Este contenido se publicó originalmente en Y! Answers, un sitio web de preguntas y respuestas que se cerró en 2021.
Loading...