在java程序中,人们将数据存储在字节数组中,前两个字节用于存储数据长度,
byte[] buffer = new byte[100000];
int size = data.getBytes().length;
buffer[0] = (byte)((size >> 8 ) & 0xff);
buffer[1] = (byte)(size & 0xff);
现在给定buffer[0]
和buffer[1]
的价值,如何让size
回来?
答案 0 :(得分:0)
很久以前,DataInputStream/DataOutputStream
java类中描述了所有这些内容。更具体地说,在你的任务中它将是:
return (short)(((int)(buffer[0] & 0xff) << 8) + ((int)(buffer[1] & 0xff) << 0));
答案 1 :(得分:0)
public int getSize(byte [] buffer)
{
return (buffer[0] << 8) | (buffer[1] << 0);
}