public long bin_to_dec() {
int leng = a.length();
for (int i = 0, j = (leng - 1); i < leng; i++, j--) {
int number = Character.getNumericValue(a.charAt(j));
result = result + (number * ((long) Math.pow(2, i)));
}
return result;
}
이 코드는 이진 문자열을 인수로 취하여 십진수 값을 반환합니다.
하지만 긴 문자열 즉매우 긴 바이너리 문자열에 대해 음수 값을 반환하는 이유는 무엇입니까?
(111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111)
는 -28을 반환합니다.
메모리가 범위를 벗어나는 이유는 무엇입니까? 또는 내 코드가 잘못 되었습니까?
http://en.wikipedia.org/wiki/Integer_overflow – iForests
"이 코드는 인수로 이진 문자열을 사용합니다": 사실,
BigInteger
이 사용할 수있는 기본 방법이있다? 그것은 나에게 niladic 보인다. –