사람은 자바18 자리 숫자 문자열을 BigInteger로 변환하는 방법?
예에서의 BigInteger에 숫자 18 자리 문자열 변환에 나를 도울 수; 문자열 "0x9999999999999999"
가 0x9999999999999999
숫자 값으로 표시됩니다.
사람은 자바18 자리 숫자 문자열을 BigInteger로 변환하는 방법?
예에서의 BigInteger에 숫자 18 자리 문자열 변환에 나를 도울 수; 문자열 "0x9999999999999999"
가 0x9999999999999999
숫자 값으로 표시됩니다.
숫자가 16 진수 일 것으로 기대하십니까? 0x는 대개 무엇을 의미합니까?
다시 BigInteger를하고에 텍스트로 16 진수 번호를 설정하는 BigInteger를BigInteger bi = new BigInteger(string);
String text = bi.toString();
에 일반 문자열을 돌립니다.
if(string.startsWith("0x")) {
BigInteger bi = new BigInteger(string.sustring(2),16);
String text = "0x" + bi.toString(16);
}
BigInteger bigInt = new BigInteger("9999999999999999", 16);
당신은 BigInteger의 생성자에 기반을 지정할 수 있습니다.
BigInteger bi = new BigInteger("9999999999999999", 16);
String s = bi.toString(16);
문자열은 항상 "0x"로 시작하는 16 진수 인 경우 :
String str = "0x9999999999999999";
BigInteger number = new BigInteger(str.substring(2));
더 나은은 "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);
}
로 시작하면, 확인이 16 진수로 표시하거나 16 진수로 변환 :
System.out.printf("0x%x%n", number);
// or
String hex = String.format("0x%x", number);
음수 값주의! – TheRealNeo
두 번째 예에서 OP가 출력되도록 요청 된 경우 아래쪽 투표가있는 이유를 모를 경우 @Carlos가 유일한 다른 방법입니다. (그는 나중에 게시했습니다.) –
음수 값도 고려하지 않습니다. – TheRealNeo
@TheRealNeo 당신이 의미하는 음수 16 진수 값의 예를 들려 줄 수 있습니까? –