Class Numbers
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classStrategy defining how bytes are combined into a number. -
Method Summary
Modifier and TypeMethodDescriptionstatic intbcd2bin(int bcd) Converts packed BCD code (1 byte, 2 BCD digits) to binary.static intbin2bcd(int bin) Converts a binary number into packed BCD (1 byte, 2 BCD digits).static StringformatBinary(int value, int bits) Formats a value as a binary number, left-padded with zeros.static StringformatBinary(int value, int bits, int groupBits) Formats a value as a binary number, left-padded with zeros, with bit groups separated by a space.static StringformatHex(int value, int digits) Formats a value as an uppercase hexadecimal number, left-padded with zeros.static Byte[]nativeBytesToBytes(byte[] array) Converts native byte[] array to boxed Byte[] array (e.g.static intparseRadix(String number) Parses an integer written in assembler-style notation.static intreadBits(byte[] bytes, int start, int length, int strategy) Reads an arbitrary number of bits (max 32) from bytes.static intCombines up to 4 memory cells into an integer.static intreverseBits(int value, int numberOfBits) Reverse bits in integer (max 32-bit) value.static longreverseBits(long value, int numberOfBits) Reverse bits in long (max 64-bit) value.static voidSplits an integer into 4 bytes.
-
Method Details
-
reverseBits
public static int reverseBits(int value, int numberOfBits) Reverse bits in integer (max 32-bit) value.- Parameters:
value- value which bits will be reversednumberOfBits- how many bits should be reversed. If the value has more bits, the rest will be preserved.- Returns:
- value with reversed bits
-
reverseBits
public static long reverseBits(long value, int numberOfBits) Reverse bits in long (max 64-bit) value.Unlike
reverseBits(int, int), higher bits ofvalueare discarded (not preserved), andnumberOfBits <= 0yields0.- Parameters:
value- value which bits will be reversednumberOfBits- how many lowest bits should be reversed- Returns:
- value with the lowest
numberOfBitsbits reversed and higher bits cleared
-
readBits
public static int readBits(byte[] bytes, int start, int length, int strategy) Reads an arbitrary number of bits (max 32) from bytes.If the strategy is LITTLE_ENDIAN, bits are read from LSB to MSB. If it is BIG_ENDIAN, bits are read from MSB to LSB.
- Parameters:
bytes- bytesstart- the bit index where reading startslength- the number of bits to readstrategy- combination ofNumbers.Strategyflags- Returns:
- the bits read
-
readInt
Combines up to 4 memory cells into an integer.Each cell represents one byte; higher-order bits of a cell are cut. If there are more than 4 cells, only the first 4 are used. If there are fewer, the missing most significant bytes are zero.
- Parameters:
word- the cells (e.g.Byte[],Short[]orInteger[])strategy- combination ofNumbers.Strategyflags- Returns:
- 32-bit value
-
writeInt
Splits an integer into 4 bytes. It is the inverse ofreadInt(Number[], int).- Parameters:
value- the valueoutput- the output; must have space for 4 bytes. Other elements are untouched.strategy- combination ofNumbers.Strategyflags
-
nativeBytesToBytes
Converts native byte[] array to boxed Byte[] array (e.g. for writing into memory).- Parameters:
array- native byte[] array- Returns:
- boxed Byte[] array
-
bcd2bin
public static int bcd2bin(int bcd) Converts packed BCD code (1 byte, 2 BCD digits) to binary.- Parameters:
bcd- packed BCD code- Returns:
- binary number
-
bin2bcd
public static int bin2bcd(int bin) Converts a binary number into packed BCD (1 byte, 2 BCD digits).- Parameters:
bin- binary number (0-99)- Returns:
- number in packed BCD code
-
parseRadix
Parses an integer written in assembler-style notation.Accepted syntax (case-insensitive, no surrounding whitespace): optional sign
+/-, followed by0xprefix: hexadecimal (0x1F)hsuffix: hexadecimal (1Fh)bsuffix or0bprefix: binary (101b,0b101)oorqsuffix: octal (17o,17q)dsuffix or no affix: decimal (12d,12; leading zeros are decimal too)
- Parameters:
number- the number text- Returns:
- parsed value
- Throws:
NumberFormatException- if the text is not a number in a recognized format, or it overflows
-
formatHex
Formats a value as an uppercase hexadecimal number, left-padded with zeros.The result is never truncated: if the value needs more digits, all are emitted (negative values have 8 digits).
- Parameters:
value- the valuedigits- minimal number of digits (e.g. 2 for a byte, 4 for a word, 8 for a dword)- Returns:
- hexadecimal string without prefix or suffix
-
formatBinary
Formats a value as a binary number, left-padded with zeros.The result is never truncated: if the value needs more bits, all are emitted (negative values have 32 bits).
- Parameters:
value- the valuebits- minimal number of bits- Returns:
- binary string without prefix or suffix
-
formatBinary
Formats a value as a binary number, left-padded with zeros, with bit groups separated by a space.Groups are counted from the least significant bit, e.g.
formatBinary(0x2F, 10, 4)is"00 0010 1111". The result is never truncated (negative values have 32 bits).- Parameters:
value- the valuebits- minimal number of bitsgroupBits- number of bits in a space-separated group; if <= 0, bits are not grouped- Returns:
- binary string without prefix or suffix
-