<bdo id='OUuOo'></bdo><ul id='OUuOo'></ul>
  • <legend id='OUuOo'><style id='OUuOo'><dir id='OUuOo'><q id='OUuOo'></q></dir></style></legend>

    <i id='OUuOo'><tr id='OUuOo'><dt id='OUuOo'><q id='OUuOo'><span id='OUuOo'><b id='OUuOo'><form id='OUuOo'><ins id='OUuOo'></ins><ul id='OUuOo'></ul><sub id='OUuOo'></sub></form><legend id='OUuOo'></legend><bdo id='OUuOo'><pre id='OUuOo'><center id='OUuOo'></center></pre></bdo></b><th id='OUuOo'></th></span></q></dt></tr></i><div id='OUuOo'><tfoot id='OUuOo'></tfoot><dl id='OUuOo'><fieldset id='OUuOo'></fieldset></dl></div>
    1. <tfoot id='OUuOo'></tfoot>

      <small id='OUuOo'></small><noframes id='OUuOo'>

      1. 从 2 或 4 字节转换为有符号/无符号 short/int

        时间:2023-07-27
          <tbody id='5td6E'></tbody>
          <bdo id='5td6E'></bdo><ul id='5td6E'></ul>

            <tfoot id='5td6E'></tfoot>
          1. <small id='5td6E'></small><noframes id='5td6E'>

              • <legend id='5td6E'><style id='5td6E'><dir id='5td6E'><q id='5td6E'></q></dir></style></legend>
                • <i id='5td6E'><tr id='5td6E'><dt id='5td6E'><q id='5td6E'><span id='5td6E'><b id='5td6E'><form id='5td6E'><ins id='5td6E'></ins><ul id='5td6E'></ul><sub id='5td6E'></sub></form><legend id='5td6E'></legend><bdo id='5td6E'><pre id='5td6E'><center id='5td6E'></center></pre></bdo></b><th id='5td6E'></th></span></q></dt></tr></i><div id='5td6E'><tfoot id='5td6E'></tfoot><dl id='5td6E'><fieldset id='5td6E'></fieldset></dl></div>
                  本文介绍了从 2 或 4 字节转换为有符号/无符号 short/int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我必须将字节转换为有符号/无符号整数或短整数.

                  I have to convert bytes to signed/unsigned int or short.

                  以下方法正确吗?哪些已签名,哪些未签名?

                  The methods below are correct? Which is signed and which unsigned?

                  字节顺序:LITTLE_ENDIAN

                  Byte order: LITTLE_ENDIAN

                  public static int convertTwoBytesToInt1(byte b1, byte b2) {
                      return (int) ((b2 << 8) | (b1 & 0xFF));
                  }
                  

                  VS.

                  public static int convertTwoBytesToInt2(byte b1, byte b2) {
                      return (int) (( (b2 & 0xFF) << 8) | (b1 & 0xFF));
                  }
                  

                  public static int  convertFourBytesToInt1(byte b1, byte b2, byte b3, byte b4){
                      return (int) ((b4<<24)+(b3<<16)+(b2<<8)+b1);
                  }
                  

                  VS.

                  public static int  convertFourBytesToInt2(byte b1, byte b2, byte b3, byte b4){
                      return (int) (( (b4 & 0xFF) << 24) | ((b3 & 0xFF) << 16) | ((b2 & 0xFF) << 8) | (b1 & 0xFF));
                  }
                  

                  对此转换表单感兴趣.谢谢!

                  I'm interested only in this conversion forms. Thanks!

                  推荐答案

                  每对的第一个方法(convertXXXToInt1())被签名,第二个方法(convertXXXToInt2()) 未签名.

                  The first method (convertXXXToInt1()) of each pair is signed, the second (convertXXXToInt2()) is unsigned.

                  然而,Java int 总是有符号的,所以如果设置了 b4 的最高位,convertFourBytesToInt2() 的结果将是负数,即使这应该是未签名"版本.

                  However, Java int is always signed, so if the highest bit of b4 is set, the result of convertFourBytesToInt2() will be negative, even though this is supposed to be the "unsigned" version.

                  假设一个byte值,b2是-1,或者十六进制的0xFF.<< 运算符将导致它被提升"为值为 -1 或 0xFFFFFFFF 的 int 类型.移位8位后为0xFFFFFF00,移位24字节后为0xFF000000.

                  Suppose a byte value, b2 is -1, or 0xFF in hexadecimal. The << operator will cause it to be "promoted" to an int type with a value of -1, or 0xFFFFFFFF. After the shift of 8 bits, it will be 0xFFFFFF00, and after a shift of 24 bytes, it will be 0xFF000000.

                  但是,如果您应用按位 & 运算符,则高位将设置为零.这将丢弃符号信息.以下是这两个案例的第一步,更详细地制定了.

                  However, if you apply the bitwise & operator, the higher-order bits will be set to zero. This discards the sign information. Here are the first steps of the two cases, worked out in more detail.

                  签名:

                  byte b2 = -1; // 0xFF
                  int i2 = b2; // 0xFFFFFFFF
                  int n = i2 << 8; // 0x0xFFFFFF00
                  

                  无符号:

                  byte b2 = -1; // 0xFF
                  int i2 = b2 & 0xFF; // 0x000000FF
                  int n = i2 << 8; // 0x0000FF00
                  

                  这篇关于从 2 或 4 字节转换为有符号/无符号 short/int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Spring ConversionService 添加转换器 下一篇:如何在 Java 中执行无符号到有符号的转换?

                  相关文章

                  最新文章

                  <i id='zMuVr'><tr id='zMuVr'><dt id='zMuVr'><q id='zMuVr'><span id='zMuVr'><b id='zMuVr'><form id='zMuVr'><ins id='zMuVr'></ins><ul id='zMuVr'></ul><sub id='zMuVr'></sub></form><legend id='zMuVr'></legend><bdo id='zMuVr'><pre id='zMuVr'><center id='zMuVr'></center></pre></bdo></b><th id='zMuVr'></th></span></q></dt></tr></i><div id='zMuVr'><tfoot id='zMuVr'></tfoot><dl id='zMuVr'><fieldset id='zMuVr'></fieldset></dl></div>
                • <small id='zMuVr'></small><noframes id='zMuVr'>

                    <legend id='zMuVr'><style id='zMuVr'><dir id='zMuVr'><q id='zMuVr'></q></dir></style></legend>
                    • <bdo id='zMuVr'></bdo><ul id='zMuVr'></ul>
                  1. <tfoot id='zMuVr'></tfoot>