public final class PrimitiveUtils extends Object
Some protocols deal with unsigned values; for example, u8
(or uint8_t
in C/C++) refers to an unsigned 8-bit value.
Java does not provide unsigned primitives, so some care needs to be taken
when dealing with such values in Java.
As an example, consider the following 8-bit value:
10010011Interpreted as an unsigned value (u8) this represents 147. A Java
byte
can hold an 8-bit value, but Java bytes
are signed (the most significant bit is the sign-bit) so this same
collection of bits, stored in a byte
, represents -109.
To interpret a u8 value correctly, it needs to be stored in the "next size up" primitve (short) so that all 8 bits are unsigned:
0000000010010011 ^ sign bitThis short value represents 147.
The fromU*() methods in this class treat their parameter as an unsigned number and return the equivalent unsigned value in the "next size up" primitive. The toU* methods in this class treat their parameter as an unsigned number and return the equivalent value "encoded" in the "next size down" primitive.
Modifier and Type | Method and Description |
---|---|
static int |
fromU16(short s)
Convert the short equivalent of an unsigned 16-bit field to an int.
|
static long |
fromU32(int i)
Convert the int equivalent of an unsigned 32-bit field to a long.
|
static BigInteger |
fromU64(long l)
Convert the long equivalent of an unsigned 64-bit field to a BigInteger.
|
static short |
fromU8(byte b)
Convert the byte equivalent of an unsigned 8-bit field to a short.
|
static short |
toU16(int uv)
Convert the given value to the short equivalent of an unsigned
16-bit field.
|
static int |
toU32(long uv)
Convert the given value to the int equivalent of an unsigned
32-bit field.
|
static long |
toU64(BigInteger uv)
Convert the given value to the long equivalent of an unsigned
64-bit field.
|
static byte |
toU8(int uv)
Convert the given value to the byte equivalent of an
unsigned 8-bit field.
|
static byte |
toU8(short uv)
Convert the given value to the byte equivalent of an
unsigned 8-bit field.
|
static void |
verifyU16(int uv)
Verifies that the given value will fit in an unsigned 16-bit field.
|
static void |
verifyU32(long uv)
Verifies that the given value will fit in an unsigned 32-bit field.
|
static void |
verifyU64(BigInteger uv)
Verifies that the given value will fit in an unsigned 64-bit field.
|
static void |
verifyU8(int uv)
Verifies that the given value will fit in an unsigned 8-bit field.
|
static void |
verifyU8(short uv)
Verifies that the given value will fit in an unsigned 8-bit field.
|
public static short fromU8(byte b)
b
- a bytepublic static void verifyU8(short uv)
uv
- unsigned valueIllegalArgumentException
- if uv is not 0..2^8-1public static byte toU8(short uv)
uv
- unsigned valueIllegalArgumentException
- if uv is not 0..2^8-1public static void verifyU8(int uv)
uv
- unsigned valueIllegalArgumentException
- if uv is not 0..2^8-1public static byte toU8(int uv)
uv
- unsigned valueIllegalArgumentException
- if uv is not 0..2^8-1public static int fromU16(short s)
s
- a shortpublic static void verifyU16(int uv)
uv
- unsigned valueIllegalArgumentException
- if uv is not 0..2^16-1public static short toU16(int uv)
uv
- unsigned valueIllegalArgumentException
- if uv is not 0..2^16-1public static long fromU32(int i)
i
- an intpublic static void verifyU32(long uv)
uv
- unsigned valueIllegalArgumentException
- if uv is not 0..2^32-1public static int toU32(long uv)
uv
- unsigned valueIllegalArgumentException
- if uv is not 0..2^32-1public static BigInteger fromU64(long l)
l
- a longpublic static void verifyU64(BigInteger uv)
uv
- unsigned valueIllegalArgumentException
- if uv is not 0..2^64-1public static long toU64(BigInteger uv)
uv
- unsigned valueIllegalArgumentException
- if uv is not 0..2^64-1Copyright © 2015. All Rights Reserved.