第一位是符号位,是不是要这样的效果?public class Test { public static void main(String args[]) { String[] s = { "11000000", "10100000", "10010000", "10011000", "10011100" }; for (int i = 0; i < s.length; i++) { byte b = (byte) Integer.valueOf(s[i], 2).intValue(); System.out.println(b); } } }
另外还问下,如果要反转呢?比如把 byte b = -127; 转换成 "11111111" 这样一个字符串该怎么弄?
public static void main(String args[]) {
String[] s = { "11000000", "10100000", "10010000", "10011000",
"10011100" };
for (int i = 0; i < s.length; i++) {
byte b = (byte) Integer.valueOf(s[i], 2).intValue();
System.out.println(b);
}
}
}
转换成 "11111111" 这样一个字符串该怎么弄?
可以这样转换:System.out.println(Integer.toBinaryString(b&255));