如何在java中连接两个字节?

如何在java中连接两个字节?,java,arrays,type-conversion,byte,bucket,Java,Arrays,Type Conversion,Byte,Bucket,我有一个名为writePos的整数,它的值介于[01023]之间。我需要将其存储在名为bucket的字节数组的最后两个字节中。所以,我想我需要将它表示为数组最后两个字节的串联 如何将writePos分解为两个字节,当连接并转换为int时,会再次生成writePos 一旦我把它分解成字节,我将如何进行连接 按位操作 到字节: byte[] bytes = new byte[2]; // This uses a bitwise and (&) to take only the last 8

我有一个名为
writePos
的整数,它的值介于
[01023]
之间。我需要将其存储在名为
bucket
的字节数组的最后两个字节中。所以,我想我需要将它表示为数组最后两个字节的串联

  • 如何将
    writePos
    分解为两个字节,当连接并转换为
    int
    时,会再次生成
    writePos

  • 一旦我把它分解成字节,我将如何进行连接

  • 按位操作

    到字节:

    byte[] bytes = new byte[2];
    // This uses a bitwise and (&) to take only the last 8 bits of i
    byte[0] = (byte)(i & 0xff); 
    // This uses a bitwise and (&) to take the 9th to 16th bits of i
    // It then uses a right shift (>>)  then move them right 8 bits
    byte[1] = (byte)((i & 0xff00) >> 8);from byte:
    
    往回走

    // This just reverses the shift, no need for masking.
    // The & here is used to handle complications coming from the sign bit that
    // will otherwise be moved as the bytes are combined together and converted
    // into an int
    i = (byte[0] & 0xFF)+(byte[1] & 0xFF)<<8;
    
    //这只是反转移位,不需要掩蔽。
    //&here用于处理来自以下符号位的复杂情况:
    //否则,将随着字节的组合和转换而移动
    //变成整数
    
    i=(字节[0]&0xFF)+(字节[1]&0xFF)您需要将整数拆分为两个字节。高字节和低字节。按照您的描述,它在数组中存储为bug endian

    int writeLocation = 511;
    byte[] bucket = new byte[10];
    // range checks must be done before
    // bitwise right rotation by 8 bits
    bucket[8] = (byte) (writeLocation >> 8); // the high byte
    bucket[9] = (byte) (writeLocation & 0xFF); // the low byte
    
    System.out.println("bytes = " + Arrays.toString(bucket));
    
    // convert back the integer value 511 from the two bytes
    bucket[8] = 1;
    bucket[9] = (byte) (0xFF);
    // the high byte will bit bitwise left rotated
    // the low byte will be converted into an int
    // and only the last 8 bits will be added
    writeLocation = (bucket[8] << 8) + (((int) bucket[9]) &  0xFF);
    System.out.println("writeLocation = " + writeLocation);
    
    int writeLocation=511;
    字节[]桶=新字节[10];
    //之前必须进行范围检查
    //按位右转8位
    bucket[8]=(字节)(写位置>>8);//高位字节
    bucket[9]=(字节)(writeLocation&0xFF);//低字节
    System.out.println(“bytes=“+Arrays.toString(bucket));
    //从两个字节中转换回整数值511
    桶[8]=1;
    bucket[9]=(字节)(0xFF);
    //高位字节将按位向左旋转
    //低位字节将转换为整数
    //并且只会添加最后8位
    
    writeLocation=(bucket[8]这将由ByteBuffer高级覆盖

    short loc = (short) writeLocation;
    
    byte[] bucket = ...
    int idex = bucket.length - 2;
    ByteBuffer buf = ByteBuffer.wrap(bucket);
    buf.order(ByteOrder.LITTLE__ENDIAN); // Optional
    buf.putShort(index, loc);
    
    writeLocation = buf.getShort(index);
    
    可以指定顺序,也可以保留默认顺序(BIG_ENDIAN)

  • ByteBuffer包装原始字节数组,并在字节数组上更改为ByteBuffer效果
  • 可以使用顺序写入和读取定位(seek),但在这里我使用重载方法对
    索引进行即时定位
  • putShort
    写入字节数组,修改两个字节,一个短字节
  • getShort
    从字节数组中读取一个short,它可以放在int中

  • 解释


    java中的
    short
    是一个两字节(有符号)整数。这就是它的意思。顺序是小字节是否:最低有效字节优先(n%256,n/256)或者big-endian。

    我不确定到底发生了什么。你介意用我的变量解释一下代码扮演什么角色吗?我会用它。注意,当一个字节被转换成int时,它会得到符号扩展,即一半的字节值被当作负数…@Bram是的,我必须快速刷新我的内存-shoul我有所有正确的班次等now@Brandon请参阅现在的答案,添加了更多解释
    字节[]字节=新字节[2];字节[0]=(字节)i;字节[1]=(字节)(i>>8)
    可以,因为从int到byte的赋值不会改变任何位。出于什么目的?如果您处理的是I/O,请查看
    DataOutputStream
    DataInputStream。
    我处理的是bucket/buffers,并试图记下写入bucket的第一个可用位置。bucket长度为1023位。所以在字节数组的末尾有两个字节(长度1023,如我所述)指出了索引的位置。我想这是我需要的答案,因为我的项目使用的是bytebuffer。不过,我不确定你的答案中是怎么回事。这是如何解释我存储桶的最后一个字节和倒数第二个字节的?添加了一个解释;java
    short
    而不是
    int
    被定义为2个字节。这很有效,谢谢。我很想对正在发生的事情有更多的了解。似乎wrap函数为您提供的字节数组添加了功能,并实际修改了它的内容。这是真的吗?您是如此正确
    wrap
    意味着传递的数据byte array是字节缓冲区的后备存储。现在提到它是b这更清楚。