2010-12-11 18 views

Respuesta

1

¿Esperas que el número esté en hexadecimal, ya que eso es lo que generalmente significa 0x?

Para convertir una cadena normal en un BigInteger

BigInteger bi = new BigInteger(string); 
String text = bi.toString(); 

para convertir un número hexadecimal como texto en un BigInteger y la espalda.

if(string.startsWith("0x")) { 
    BigInteger bi = new BigInteger(string.sustring(2),16); 
    String text = "0x" + bi.toString(16); 
} 
+0

No estoy seguro de por qué hay un voto negativo, dado que el segundo ejemplo produce el pedido de producto OP, @Carlos es el único que sí lo hace. (Publicó más tarde) –

+0

Esto tampoco considera los valores negativos – TheRealNeo

+0

@TheRealNeo ¿Puede dar un ejemplo de un valor hexadecimal negativo que quiere decir? –

0
BigInteger bigInt = new BigInteger("9999999999999999", 16);  
12

Puede especificar la base en BigInteger constructor.

BigInteger bi = new BigInteger("9999999999999999", 16); 
String s = bi.toString(16); 
2

si la cadena comienza siempre con "0x" y es hexadecimal:

String str = "0x9999999999999999"; 
    BigInteger number = new BigInteger(str.substring(2)); 

mejor, comprobar si se comienza con "0x"

String str = "0x9999999999999999"; 
    BigInteger number; 
    if (str.startsWith("0x")) { 
     number = new BigInteger(str.substring(2), 16); 
    } else { 
     // Error handling: throw NumberFormatException or something similar 
     // or try as decimal: number = new BigInteger(str); 
    } 

A la salida se como hexadecimal o convertir a una representación hexadecimal:

System.out.printf("0x%x%n", number); 
    // or 
    String hex = String.format("0x%x", number); 
+0

¡Tenga cuidado con los valores negativos aquí! – TheRealNeo

Cuestiones relacionadas