没多少c币,探讨下吧,凑字数,凑字数,凑字数,凑字数,凑字数、凑字数
这一个字节数组照着这个方法走一遍就明白了。
举例:int数291的字节数组为【00000000 00000000 00000001 00100011 】
bytes[3] & 0xFF = 00100011 & 11111111 =
00100011 ;
bytes[2]<<8 & 0xFF00 = 00000001 00000000 & 11111111 00000000 =
00000001 00000000 ;
bytes[1]<<16 & 0xFF0000 = 00000000 00000000 00000000 & 11111111 00000000 00000000=
00000000 00000000 00000000;
bytes[0]<<24 & 0xFF0000 = 00000000 00000000 00000000 00000000 & 11111111 00000000 00000000 00000000=
00000000 00000000 00000000 00000000;
int num = (int) bytes[3] & 0xFF | bytes[2]<<8 & 0xFF00 | bytes[1]<<16 & 0xFF0000 | bytes[0]<<24 & 0xFF0000 =
00000000 00000000 00000001 00100011;
num=291;
这里主要用到了位运算符 << , & ,| .希望对您有帮助。
这是个有关字节序的转换,在x86处理下,数据都是以小端的形式存放数据;而网络中是以大端的形式存放数据;楼主去有关大端和小端,就会明白了。
int型数据所占用的字节为4字节,通过bytes[0]~bytes[3]位运算可以取得bytes[0]~bytes[3]的值,
字节序转换,大小端转换。
http://blog.csdn.net/wojiuai2093/article/details/50779879
int是32位的 byte是8位的
把int转换为byte,这个操作会将int的低8位(从右向左数8位)存到byte类型中于是,byte中就存了