Convert from 2 or 4 bytes to signed/unsigned short/int

断了今生、忘了曾经 提交于 2019-12-18 13:25:08

问题


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

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

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));
}

and

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!


回答1:


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

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.

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.

Signed:

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

Unsigned:

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



回答2:


There is a problem with the 4-byte unsigned conversion, because it doesn't fit into an int. The routines below work correctly.

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

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

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

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

  public static void main (String[] args)
  {
    byte b1 = (byte) 0xFF;
    byte b2 = (byte) 0xFF;
    byte b3 = (byte) 0xFF;
    byte b4 = (byte) 0xFF;

    System.out.printf ("%,14d%n", convertTwoBytesToInt1 (b1, b2));
    System.out.printf ("%,14d%n", convertTwoBytesToInt2 (b1, b2));

    System.out.printf ("%,14d%n", convertFourBytesToInt1 (b1, b2, b3, b4));
    System.out.printf ("%,14d%n", convertFourBytesToInt2 (b1, b2, b3, b4));
  }
}


来源:https://stackoverflow.com/questions/10803557/convert-from-2-or-4-bytes-to-signed-unsigned-short-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!