如何使用 java 反转缓冲区内的字节
how to reverse the bytes inside the buffer using java
我有一个缓冲区包含两个字节让我们想象缓冲区是:org.jboss.netty.buffer.ChannelBuffer
缓冲区[28,29,30,31,32]到
读取 java 中的两个第一个字节,我们使用这个函数:
buffer.readShort()
但我想做的是将缓冲区从 29 读取到 28(我想反转字节的顺序)。
由于这不是您的对象,您必须使用 readByte
自己阅读 Byte
。
对于 Short,创建一个 Byte[2]
的数组并调用该方法两次来填充它:
byte[] shortByte = {
channel.readByte(),
channel.readByte()
}
然后反过来或更简单
byte[] shortByte = new byte[2];
shortByte[1] = channel.readByte();
shortByte[0] = channel.readByte();
然后,从这里开始,您只需要从这个数组创建 Short。你可以从下面的 post 看到如何: Convert a byte array to integer in java and vice versa
我有一个缓冲区包含两个字节让我们想象缓冲区是:org.jboss.netty.buffer.ChannelBuffer
缓冲区[28,29,30,31,32]到
读取 java 中的两个第一个字节,我们使用这个函数:
buffer.readShort()
但我想做的是将缓冲区从 29 读取到 28(我想反转字节的顺序)。
由于这不是您的对象,您必须使用 readByte
自己阅读 Byte
。
对于 Short,创建一个 Byte[2]
的数组并调用该方法两次来填充它:
byte[] shortByte = {
channel.readByte(),
channel.readByte()
}
然后反过来或更简单
byte[] shortByte = new byte[2];
shortByte[1] = channel.readByte();
shortByte[0] = channel.readByte();
然后,从这里开始,您只需要从这个数组创建 Short。你可以从下面的 post 看到如何: Convert a byte array to integer in java and vice versa