62,620
社区成员
发帖
与我相关
我的任务
分享
byte[] message = "需要传输的字符串".getByte();
int length = message.length;
ByteBuffer buf = ByteBuffer.allocate(length + 4);
buf.setAutoExpand(true);
byte[] lens = new byte[4];
lens[0] = (byte) ((0xFF000000 & length) >> 24);
lens[1] = (byte) ((0x00FF0000 & length) >> 16);
lens[2] = (byte) ((0x0000FF00 & length) >> 8);
lens[3] = (byte) (0x000000FF & length);
buf.put(lens);
buf.put();
buf.flip();