Can we make unsigned byte in Java

前端 未结 16 1002
青春惊慌失措
青春惊慌失措 2020-11-22 08:02

I am trying to convert a signed byte in unsigned. The problem is the data I am receiving is unsigned and Java does not support unsigned byte, so when it reads the data it tr

相关标签:
16条回答
  • 2020-11-22 08:26

    If think you are looking for something like this.

    public static char toUnsigned(byte b) {
        return (char) (b >= 0 ? b : 256 + b);
    }
    
    0 讨论(0)
  • 2020-11-22 08:26

    You can also:

    public static int unsignedToBytes(byte a)
    {
        return (int) ( ( a << 24) >>> 24);
    }    
    

    Explanation:

    let's say a = (byte) 133;

    In memory it's stored as: "1000 0101" (0x85 in hex)

    So its representation translates unsigned=133, signed=-123 (as 2's complement)

    a << 24

    When left shift is performed 24 bits to the left, the result is now a 4 byte integer which is represented as:

    "10000101 00000000 00000000 00000000" (or "0x85000000" in hex)

    then we have

    ( a << 24) >>> 24

    and it shifts again on the right 24 bits but fills with leading zeros. So it results to:

    "00000000 00000000 00000000 10000101" (or "0x00000085" in hex)

    and that is the unsigned representation which equals to 133.

    If you tried to cast a = (int) a; then what would happen is it keeps the 2's complement representation of byte and stores it as int also as 2's complement:

    (int) "10000101" ---> "11111111 11111111 11111111 10000101"

    And that translates as: -123

    0 讨论(0)
  • 2020-11-22 08:28

    If you want unsigned bytes in Java, just subtract 256 from the number you're interested in. It will produce two's complement with a negative value, which is the desired number in unsigned bytes.

    Example:

    int speed = 255; //Integer with the desired byte value
    byte speed_unsigned = (byte)(speed-256);
    //This will be represented in two's complement so its binary value will be 1111 1111
    //which is the unsigned byte we desire.
    

    You need to use such dirty hacks when using leJOS to program the NXT brick.

    0 讨论(0)
  • 2020-11-22 08:29

    I think the other answers have covered memory representation and how you handle these depends on the context of how you plan on using it. I'll add that Java 8 added some support for dealing with unsigned types. In this case, you could use Byte.toUnsignedInt

    int unsignedInt = Byte.toUnsignedInt(myByte);
    
    0 讨论(0)
  • 2020-11-22 08:31

    I am trying to use this data as a parameter to a function of Java that accepts only a byte as parameter

    This is not substantially different from a function accepting an integer to which you want to pass a value larger than 2^32-1.

    That sounds like it depends on how the function is defined and documented; I can see three possibilities:

    1. It may explicitly document that the function treats the byte as an unsigned value, in which case the function probably should do what you expect but would seem to be implemented wrong. For the integer case, the function would probably declare the parameter as an unsigned integer, but that is not possible for the byte case.

    2. It may document that the value for this argument must be greater than (or perhaps equal to) zero, in which case you are misusing the function (passing an out-of-range parameter), expecting it to do more than it was designed to do. With some level of debugging support you might expect the function to throw an exception or fail an assertion.

    3. The documentation may say nothing, in which case a negative parameter is, well, a negative parameter and whether that has any meaning depends on what the function does. If this is meaningless then perhaps the function should really be defined/documented as (2). If this is meaningful in an nonobvious manner (e.g. non-negative values are used to index into an array, and negative values are used to index back from the end of the array so -1 means the last element) the documentation should say what it means and I would expect that it isn't what you want it to do anyway.

    0 讨论(0)
  • 2020-11-22 08:32

    Adamski provided the best answer, but it is not quite complete, so read his reply, as it explains the details I'm not.

    If you have a system function that requires an unsigned byte to be passed to it, you can pass a signed byte as it will automatically treat it as an unsigned byte.

    So if a system function requires four bytes, for example, 192 168 0 1 as unsigned bytes you can pass -64 -88 0 1, and the function will still work, because the act of passing them to the function will un-sign them.

    However you are unlikely to have this problem as system functions are hidden behind classes for cross-platform compatibility, though some of the java.io read methods return unsighed bytes as an int.

    If you want to see this working, try writing signed bytes to a file and read them back as unsigned bytes.

    0 讨论(0)
提交回复
热议问题