2010-12-11 25 views

Répondre

1

Pensez-vous que le nombre soit en hexadécimal, puisque c'est ce que 0x signifie habituellement?

Pour transformer une chaîne simple dans un BigInteger

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

pour transformer un numéro hexadécimal sous forme de texte dans un BigInteger et le dos.

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

Je ne sais pas pourquoi il y a un vote en baisse étant donné que le deuxième exemple produit l'OP demandé pour la sortie, @Carlos est le seul autre qui le fait. (Il a posté plus tard) –

+0

Cela ne considère pas non plus les valeurs négatives – TheRealNeo

+0

@TheRealNeo pouvez-vous donner un exemple d'une valeur hexadécimale négative que vous voulez dire? –

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

Vous pouvez spécifier la base dans le constructeur BigInteger.

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

Si la chaîne commence toujours par "0x" et est hexadécimal:

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

mieux, vérifier si elle commence par "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); 
    } 

Pour le sortir comme hexadécimal ou convertir en représentation hexadécimale:

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

Attention aux valeurs négatives ici! – TheRealNeo