public final class DataTypes
extends java.lang.Object
This class has been verified against binary data written using the C programming language.
To demonstrate how you can use this class, consider the following example:
//Retrieve a byte array from somewhere, that contains a uint16_t C type:
byte[] bytes0 = ...;
//Convert the byte array with a uint16_t C type to an int:
int i = DataTypes.fromUInt16ToSignedInt(bytes0);
//Convert the int back to a uint16_t C type, in the form of an array:
byte[] bytes1 = DataTypes.fromSignedIntToUInt16(i);
The endianness of Java is Big Endian. So that's the endianness implied, unless otherwise specified. The methods provided in this class will have the endianness appended after the type if it differs from Big Endian.
Here you will find a few examples:
fromSignedIntToInt16LittleEndian
: This method is implied to mean fromSignedIntBigEndianToInt16LittleEndian
.fromInt16LittleEndianToSignedInt
: This method is implied to mean fromInt16LittleEndianToSignedIntBigEndian
.Modifier and Type | Method and Description |
---|---|
static byte[] |
fromBigIntegerToUInt64(java.math.BigInteger bigInteger)
Converts the given
BigInteger to a byte array denoting a C type called uint64_t . |
static int |
fromInt16ToSignedInt(byte[] bytes)
Converts the C type
int16_t provided by the array bytes to a signed int . |
static int |
fromInt16ToSignedInt(byte[] bytes,
int offset)
Converts the C type
int16_t provided by the array bytes to a signed int . |
static int |
fromInt16ToSignedInt(byte a,
byte b) |
static int |
fromInt32LittleEndianToSignedInt(byte[] bytes) |
static int |
fromInt32LittleEndianToSignedInt(byte[] bytes,
int offset) |
static int |
fromInt32LittleEndianToSignedInt(byte[] bytes,
int offset,
boolean isAnding) |
static int |
fromInt32ToSignedInt(byte[] bytes)
Converts the C type
int32_t provided by the array bytes to a signed int . |
static int |
fromInt32ToSignedInt(byte[] bytes,
int offset)
Converts the C type
int32_t provided by the array bytes to a signed int . |
static int |
fromInt32ToSignedInt(byte a,
byte b,
byte c,
byte d) |
static long |
fromInt64ToSignedLong(byte[] bytes) |
static long |
fromInt64ToSignedLong(byte[] bytes,
int offset) |
static long |
fromInt64ToSignedLong(byte a,
byte b,
byte c,
byte d,
byte e,
byte f,
byte g,
byte h) |
static int |
fromInt8ToSignedInt(byte a) |
static int |
fromInt8ToSignedInt(byte[] bytes)
Converts the C type
int8_t provided by the array bytes to a signed int . |
static int |
fromInt8ToSignedInt(byte[] bytes,
int offset)
Converts the C type
int8_t provided by the array bytes to a signed int . |
static int |
fromInt8ToSignedInt(byte[] bytes,
int offset,
boolean isAnding) |
static byte[] |
fromSignedIntToInt16(int i)
Converts the given signed
int to a byte array denoting a C type called int16_t . |
static byte[] |
fromSignedIntToInt16LittleEndian(int i) |
static byte[] |
fromSignedIntToInt32(int i)
Converts the given signed
int to a byte array denoting a C type called int32_t . |
static byte[] |
fromSignedIntToInt32LittleEndian(int i) |
static byte[] |
fromSignedIntToInt8(int i)
Converts the given signed
int to a byte array denoting a C type called int8_t . |
static byte[] |
fromSignedIntToUInt16(int i)
Converts the given signed
int to a byte array denoting a C type called uint16_t . |
static byte[] |
fromSignedIntToUInt8(int i)
Converts the given signed
int to a byte array denoting a C type called uint8_t . |
static byte[] |
fromSignedLongToInt64(long l)
Converts the given signed
long to a byte array denoting a C type called uint64_t . |
static byte[] |
fromSignedLongToUInt32(long l)
Converts the given signed
long to a byte array denoting a C type called uint32_t . |
static int |
fromUInt16ToSignedInt(byte[] bytes) |
static int |
fromUInt16ToSignedInt(byte[] bytes,
int offset) |
static int |
fromUInt16ToSignedInt(byte a,
byte b) |
static int |
fromUInt16ToSignedInt(int a,
int b) |
static int |
fromUInt32ToSignedInt(byte[] bytes) |
static int |
fromUInt32ToSignedInt(byte[] bytes,
int offset) |
static int |
fromUInt32ToSignedInt(byte a,
byte b,
byte c,
byte d) |
static long |
fromUInt32ToSignedLong(byte[] bytes) |
static long |
fromUInt32ToSignedLong(byte[] bytes,
int offset) |
static long |
fromUInt32ToSignedLong(byte a,
byte b,
byte c,
byte d) |
static java.math.BigInteger |
fromUInt64ToBigInteger(byte[] bytes) |
static java.math.BigInteger |
fromUInt64ToBigInteger(byte[] bytes,
int offset) |
static java.math.BigInteger |
fromUInt64ToBigInteger(byte a,
byte b,
byte c,
byte d,
byte e,
byte f,
byte g,
byte h) |
static int |
fromUInt8ToSignedInt(byte a) |
static int |
fromUInt8ToSignedInt(byte[] bytes) |
static int |
fromUInt8ToSignedInt(byte[] bytes,
int offset) |
static char |
fromUInt8ToUnsignedChar(byte[] bytes) |
static char |
fromUInt8ToUnsignedChar(byte[] bytes,
int offset) |
static java.math.BigInteger |
fromUIntXYToBigInteger(byte... bytes) |
static java.math.BigInteger |
fromUIntXYToBigInteger(byte[] bytes,
int offset,
int size) |
static boolean |
isBitSet(int value,
int index) |
static boolean |
isBitSet(int value,
int index,
boolean isInversed) |
static int |
readUInt16AsSignedInt(java.io.InputStream inputStream) |
static int |
readUInt32AsSignedInt(java.io.InputStream inputStream) |
static int |
readUInt8AsSignedInt(java.io.InputStream inputStream) |
static char |
readUInt8AsUnsignedChar(java.io.InputStream inputStream) |
static int |
setBit(int value,
int index) |
static int |
setBit(int value,
int index,
boolean isSet) |
static int |
setBit(int value,
int index,
boolean isSet,
boolean isInversed) |
static int |
toggleBit(int value,
int index) |
static int |
unsetBit(int value,
int index) |
public static java.math.BigInteger fromUInt64ToBigInteger(byte a, byte b, byte c, byte d, byte e, byte f, byte g, byte h)
public static java.math.BigInteger fromUInt64ToBigInteger(byte[] bytes)
public static java.math.BigInteger fromUInt64ToBigInteger(byte[] bytes, int offset)
public static java.math.BigInteger fromUIntXYToBigInteger(byte... bytes)
public static java.math.BigInteger fromUIntXYToBigInteger(byte[] bytes, int offset, int size)
public static boolean isBitSet(int value, int index)
public static boolean isBitSet(int value, int index, boolean isInversed)
public static byte[] fromBigIntegerToUInt64(java.math.BigInteger bigInteger)
BigInteger
to a byte
array denoting a C type called uint64_t
.
Returns an array with length 8
.
If bigInteger
is null
, a NullPointerException
will be thrown.
bigInteger
- the BigInteger
that contains the data to convert8
java.lang.NullPointerException
- thrown if, and only if, bigInteger
is null
public static byte[] fromSignedIntToInt8(int i)
int
to a byte
array denoting a C type called int8_t
.
Returns an array with length 1
.
i
- the int
that contains the data to convert1
public static byte[] fromSignedIntToInt16(int i)
int
to a byte
array denoting a C type called int16_t
.
Returns an array with length 2
.
i
- the int
that contains the data to convert2
public static byte[] fromSignedIntToInt16LittleEndian(int i)
public static byte[] fromSignedIntToInt32(int i)
int
to a byte
array denoting a C type called int32_t
.
Returns an array with length 4
.
i
- the int
that contains the data to convert4
public static byte[] fromSignedIntToInt32LittleEndian(int i)
public static byte[] fromSignedIntToUInt8(int i)
int
to a byte
array denoting a C type called uint8_t
.
Returns an array with length 1
.
i
- the int
that contains the data to convert1
public static byte[] fromSignedIntToUInt16(int i)
int
to a byte
array denoting a C type called uint16_t
.
Returns an array with length 2
.
i
- the int
that contains the data to convert2
public static byte[] fromSignedLongToUInt32(long l)
long
to a byte
array denoting a C type called uint32_t
.
Returns an array with length 4
.
l
- the long
that contains the data to convert4
public static byte[] fromSignedLongToInt64(long l)
long
to a byte
array denoting a C type called uint64_t
.
Returns an array with length 8
.
l
- the long
that contains the data to convert8
public static char fromUInt8ToUnsignedChar(byte[] bytes)
public static char fromUInt8ToUnsignedChar(byte[] bytes, int offset)
public static char readUInt8AsUnsignedChar(java.io.InputStream inputStream) throws java.io.IOException
java.io.IOException
public static int fromInt8ToSignedInt(byte a)
public static int fromInt8ToSignedInt(byte[] bytes)
int8_t
provided by the array bytes
to a signed int
.
The length of bytes
must be at least 1
.
This method calls fromInt8ToSignedInt(byte[], int)
with 0
as offset.
If bytes
is null
, a NullPointerException
will be thrown.
If bytes.length
is less than 1
, an ArrayIndexOutOfBoundsException
will be thrown.
bytes
- the byte
array to convertbytes
java.lang.ArrayIndexOutOfBoundsException
- thrown if, and only if, bytes.length
is less than 1
java.lang.NullPointerException
- thrown if, and only if, bytes
is null
public static int fromInt8ToSignedInt(byte[] bytes, int offset)
int8_t
provided by the array bytes
to a signed int
.
The length of bytes
must be at least offset + 1
, where offset
is a positive number.
If bytes
is null
, a NullPointerException
will be thrown.
If bytes.length
is less than offset + 1
, an ArrayIndexOutOfBoundsException
will be thrown.
bytes
- the byte
array to convertoffset
- the offset to start at in the arraybytes
java.lang.ArrayIndexOutOfBoundsException
- thrown if, and only if, bytes.length
is less than offset + 1
java.lang.NullPointerException
- thrown if, and only if, bytes
is null
public static int fromInt8ToSignedInt(byte[] bytes, int offset, boolean isAnding)
public static int fromInt16ToSignedInt(byte a, byte b)
public static int fromInt16ToSignedInt(byte[] bytes)
int16_t
provided by the array bytes
to a signed int
.
The length of bytes
must be at least 2
.
This method calls fromInt16ToSignedInt(byte[], int)
with 0
as offset.
If bytes
is null
, a NullPointerException
will be thrown.
If bytes.length
is less than 2
, an ArrayIndexOutOfBoundsException
will be thrown.
bytes
- the byte
array to convertbytes
java.lang.ArrayIndexOutOfBoundsException
- thrown if, and only if, bytes.length
is less than 2
java.lang.NullPointerException
- thrown if, and only if, bytes
is null
public static int fromInt16ToSignedInt(byte[] bytes, int offset)
int16_t
provided by the array bytes
to a signed int
.
The length of bytes
must be at least offset + 2
, where offset
is a positive number.
If bytes
is null
, a NullPointerException
will be thrown.
If bytes.length
is less than offset + 2
, an ArrayIndexOutOfBoundsException
will be thrown.
bytes
- the byte
array to convertoffset
- the offset to start at in the arraybytes
java.lang.ArrayIndexOutOfBoundsException
- thrown if, and only if, bytes.length
is less than offset + 2
java.lang.NullPointerException
- thrown if, and only if, bytes
is null
public static int fromInt32ToSignedInt(byte a, byte b, byte c, byte d)
public static int fromInt32ToSignedInt(byte[] bytes)
int32_t
provided by the array bytes
to a signed int
.
The length of bytes
must be at least 4
.
This method calls fromInt32ToSignedInt(byte[], int)
with 0
as offset.
If bytes
is null
, a NullPointerException
will be thrown.
If bytes.length
is less than 4
, an ArrayIndexOutOfBoundsException
will be thrown.
bytes
- the byte
array to convertbytes
java.lang.ArrayIndexOutOfBoundsException
- thrown if, and only if, bytes.length
is less than 4
java.lang.NullPointerException
- thrown if, and only if, bytes
is null
public static int fromInt32ToSignedInt(byte[] bytes, int offset)
int32_t
provided by the array bytes
to a signed int
.
The length of bytes
must be at least offset + 4
, where offset
is a positive number.
If bytes
is null
, a NullPointerException
will be thrown.
If bytes.length
is less than offset + 4
, an ArrayIndexOutOfBoundsException
will be thrown.
bytes
- the byte
array to convertoffset
- the offset to start at in the arraybytes
java.lang.ArrayIndexOutOfBoundsException
- thrown if, and only if, bytes.length
is less than offset + 4
java.lang.NullPointerException
- thrown if, and only if, bytes
is null
public static int fromInt32LittleEndianToSignedInt(byte[] bytes)
public static int fromInt32LittleEndianToSignedInt(byte[] bytes, int offset)
public static int fromInt32LittleEndianToSignedInt(byte[] bytes, int offset, boolean isAnding)
public static int fromUInt8ToSignedInt(byte a)
public static int fromUInt8ToSignedInt(byte[] bytes)
public static int fromUInt8ToSignedInt(byte[] bytes, int offset)
public static int fromUInt16ToSignedInt(byte a, byte b)
public static int fromUInt16ToSignedInt(byte[] bytes)
public static int fromUInt16ToSignedInt(byte[] bytes, int offset)
public static int fromUInt16ToSignedInt(int a, int b)
public static int fromUInt32ToSignedInt(byte a, byte b, byte c, byte d)
public static int fromUInt32ToSignedInt(byte[] bytes)
public static int fromUInt32ToSignedInt(byte[] bytes, int offset)
public static int readUInt8AsSignedInt(java.io.InputStream inputStream) throws java.io.IOException
java.io.IOException
public static int readUInt16AsSignedInt(java.io.InputStream inputStream) throws java.io.IOException
java.io.IOException
public static int readUInt32AsSignedInt(java.io.InputStream inputStream) throws java.io.IOException
java.io.IOException
public static int setBit(int value, int index)
public static int setBit(int value, int index, boolean isSet)
public static int setBit(int value, int index, boolean isSet, boolean isInversed)
public static int toggleBit(int value, int index)
public static int unsetBit(int value, int index)
public static long fromInt64ToSignedLong(byte a, byte b, byte c, byte d, byte e, byte f, byte g, byte h)
public static long fromInt64ToSignedLong(byte[] bytes)
public static long fromInt64ToSignedLong(byte[] bytes, int offset)
public static long fromUInt32ToSignedLong(byte a, byte b, byte c, byte d)
public static long fromUInt32ToSignedLong(byte[] bytes)
public static long fromUInt32ToSignedLong(byte[] bytes, int offset)