java 端接到后放在一个 long 的低32位就OK,long 是64位的。 你回传的时候也只需要回传long值的低32位。
ByteArrayOutputStream os = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeInt(i); byte[] buff = os.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(recvbytes); DataInputStream dis = new DataInputStream(bais); int i = dis.readInt();
(1) int -> byte[4]
byte[] bs = new byte[4];
bs[0] = (byte)(i>>24 & 0x000000FF);
bs[1] = (byte)(i>>16 & 0x000000FF);
bs[2] = (byte)(i>>8 & 0x000000FF);
bs[3] = (byte)(i & 0x000000FF);
(2) byte[4] -> outstream
ByteArrayOutputStream os = new ByteArrayOutputStream();
os.write(bs);
(3) outstream -> inputstream
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
(4) inputstream -> byte[4]
byte[] b = new byte[4];
is.read(b);
(5) byte[4] -> int
int r = b[0]<<24 & 0xFF000000 |
b[1]<<16 & 0x00FF0000 |
b[2]<<8 & 0x0000FF00 |
b[3] & 0x000000FF;
你回传的时候也只需要回传long值的低32位。
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(i);
byte[] buff = os.toByteArray();
DataInputStream dis = new DataInputStream(bais);
int i = dis.readInt();
我发送的消息有无符号整数和定长字符串两种类型,是不是不能用你说的方法?