关于大端存储跟小端存储
1.计算机内存的两种存储方式
(1)大端模式
(2)小端模式
2.两者区别
大端模式是高位存在低地址,低位存在高地址;而小端模式则相反。
例如:0x1234;内存地址 2000 2001
对于大端模式 12存在2000,34存在2001
对于小端模式,34存在2000,12存在2001
3.对于网络上使用的是大端模式传输 ,也就是必须先把高字节的发送出去,接收到的也是也是高字节优先。
例如 在网络上发送一个int和接收一个int
发送:
public void writeInt (int data)
{
writeBytes (new byte[] {
(byte)(data >> 24),
(byte)(data >> 16),
(byte)(data >> 8),
(byte)data
});
}
接收:
public int readInt ()
{
int i = buffers [readerIndex] << 24 | buffers [readerIndex + 1] << 16 | buffers [readerIndex + 2] << 8 | buffers [readerIndex + 3];
readerIndex += 4;
return i;
}