Integer.parseIntã«ããè² ã®2é²æ°è§£æ
Javaã§2é²æ°æååãintå¤ã«ãããã¨æã£ãããInteger#parseInt(s, 2) ãããã°ããã®ã ãããª
public class Test { public static void main(String[] args) { String s = "10000000000000000000000000000000"; // 32 length characters int i = Integer.parseInt(s, 2); // NumberFormatException } }
public class Test { public static void main(String[] args) { String s = "-10000000000000000000000000000000"; // 33 length characters int i = Integer.parseInt(s, 2); System.out.println(Integer.MIN_VALUE == i); // true } }
ããããããã
ãåèã
http://stackoverflow.com/questions/8888946/converting-32-bit-binary-string-with-integer-parseint-fails
ãããªããæ¹ã¯ééããã«æ±ºã¾ã£ã¦ãã
ééããªãæ¹ããããã
ããé¢åã ãã以ä¸ã®èªåå®è£ ã®æ¹ãããã¶ããã·ã«æãã
public class Test { public static void main(String[] args) { String s = "10000000000000000000000000000000"; int i = bin2Int(s); System.out.println(Integer.MIN_VALUE == i); // true } private static int bin2Int(String s) { if (32 < s.length() || 0 == s.length()) { throw new NumberFormatException(); } int i = 0; for (char c : s.toCharArray()) { i <<= 1; switch (c) { case '0': break; case '1': i |= 1; break; default: throw new NumberFormatException(); } } return i; } }