从byte []中提取ID

时间:2011-11-23 14:45:53

标签: java sockets network-programming mask

我从套接字byte[]读取Param_Code,其中包含ID。

byte[] cbuf = new byte[4];
socketReader.read(cbuf, 0, 4);
int Param_Code = byteArrayToIntBI(cbuf, 0);

public static int byteArrayToIntBI(byte[] b, int offset) {
        int value = 0;
        for (int i = 3; i > -1; i--) {
            int shift = (i) * 8;
            value += (b[i + offset] & 0x000000FF) << shift;
        }
    return value;
   }

•选项1:有时Param_code仅对应于ID

•选项2:有时Param_code对应于0x40000000 + ID

•选项3:有时Param_code对应于0x80000000 + ID

我的问题是,如果考虑上述选项,我如何从Param_code获取ID。

1 个答案:

答案 0 :(得分:0)

如何使用ByteBuffer?

byte[] cbuf = new byte[4];
socketReader.read(cbuf, 0, 4);
ByteBuffer buffer = ByteBuffer.wrap(cbuf);
int Param_Code = buffer.getInt();
相关问题