62,620
社区成员
发帖
与我相关
我的任务
分享

Integer i=32767;
System.out.println(i.toBinaryString(i).length());
char [] arr={' ','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E'};
StringBuffer sb=new StringBuffer();
while(i!=0){
System.out.println((i>>4)&0x0f);
char c=arr[(i>>4)&0x0f];
sb.append(c);
i>>=4;
}
System.out.println(sb.toString());
}
打印的结果为 EE6,只用到了十二位
public class Test {
public static void main(String[] args) {
byte[] b = getBytes(32769);
System.out.printf("%d,%d\n", b[0], b[1]);
int i = getInt(b);
System.out.println(i);
}
public static byte[] getBytes(int data) {
byte[] bytes = new byte[2];
bytes[1] = (byte) (data & 0xff);
bytes[0] = (byte) ((data & 0xff00) >> 8);
return bytes;
}
public static int getInt(byte[] bytes) {
return (int) ((0xff & bytes[1]) | (0xff00 & (bytes[0] << 8)));
}
}
我2B了 突然想起来32767是2的15次方-1