Java Bit Unpack unpackUnsignedByte(byte b)

Here you can find the source of unpackUnsignedByte(byte b)

Description

Since byte is a signed type you cannot receive the unsigned value even if you do (byte) 200 .

License

Apache License

Parameter

Parameter Description
b value in range -128 to 127

Return

value in range 0 to 255

Declaration

public static int unpackUnsignedByte(byte b) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from w  w  w  .j  av a  2  s  .c  om*/
     * Since byte is a signed type you cannot receive the unsigned value
     * even if you do {@code (byte) 200}. This method will return {@code 200}
     * in this case instead of a negative one.
     * <p>
     * Applied mapping:
     * [0..127] to [0..127] and [-128..-1] to [128..255] 
     * 
     * @param b value in range -128 to 127
     * @return value in range 0 to 255
     */
    public static int unpackUnsignedByte(byte b) {
        // byte: 0 - 127 => 0 - 127, 128 - 255 => -128 - -1
        return b >= 0 ? b : b + 256;
    }
}

Related

  1. unpackByte(String bits)
  2. unpackBytes(String src)