Class Numbers

java.lang.Object
net.emustudio.emulib.Numbers

@ThreadSafe public final class Numbers extends Object
Parsing, formatting, bit, byte-order and BCD operations on numbers and memory cells.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Strategy defining how bytes are combined into a number.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    bcd2bin(int bcd)
    Converts packed BCD code (1 byte, 2 BCD digits) to binary.
    static int
    bin2bcd(int bin)
    Converts a binary number into packed BCD (1 byte, 2 BCD digits).
    static String
    formatBinary(int value, int bits)
    Formats a value as a binary number, left-padded with zeros.
    static String
    formatBinary(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 String
    formatHex(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 int
    Parses an integer written in assembler-style notation.
    static int
    readBits(byte[] bytes, int start, int length, int strategy)
    Reads an arbitrary number of bits (max 32) from bytes.
    static int
    readInt(Number[] word, int strategy)
    Combines up to 4 memory cells into an integer.
    static int
    reverseBits(int value, int numberOfBits)
    Reverse bits in integer (max 32-bit) value.
    static long
    reverseBits(long value, int numberOfBits)
    Reverse bits in long (max 64-bit) value.
    static void
    writeInt(int value, Byte[] output, int strategy)
    Splits an integer into 4 bytes.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 reversed
      numberOfBits - 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 of value are discarded (not preserved), and numberOfBits <= 0 yields 0.

      Parameters:
      value - value which bits will be reversed
      numberOfBits - how many lowest bits should be reversed
      Returns:
      value with the lowest numberOfBits bits 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 - bytes
      start - the bit index where reading starts
      length - the number of bits to read
      strategy - combination of Numbers.Strategy flags
      Returns:
      the bits read
    • readInt

      public static int readInt(Number[] word, int strategy)
      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[] or Integer[])
      strategy - combination of Numbers.Strategy flags
      Returns:
      32-bit value
    • writeInt

      public static void writeInt(int value, Byte[] output, int strategy)
      Splits an integer into 4 bytes. It is the inverse of readInt(Number[], int).
      Parameters:
      value - the value
      output - the output; must have space for 4 bytes. Other elements are untouched.
      strategy - combination of Numbers.Strategy flags
    • nativeBytesToBytes

      public static Byte[] nativeBytesToBytes(byte[] array)
      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

      public static int parseRadix(String number) throws NumberFormatException
      Parses an integer written in assembler-style notation.

      Accepted syntax (case-insensitive, no surrounding whitespace): optional sign +/-, followed by

      • 0x prefix: hexadecimal (0x1F)
      • h suffix: hexadecimal (1Fh)
      • b suffix or 0b prefix: binary (101b, 0b101)
      • o or q suffix: octal (17o, 17q)
      • d suffix or no affix: decimal (12d, 12; leading zeros are decimal too)
      The result must fit into a signed 32-bit integer.
      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

      public static String formatHex(int value, int digits)
      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 value
      digits - 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

      public static String formatBinary(int value, int bits)
      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 value
      bits - minimal number of bits
      Returns:
      binary string without prefix or suffix
    • formatBinary

      public static String formatBinary(int value, int bits, int groupBits)
      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 value
      bits - minimal number of bits
      groupBits - number of bits in a space-separated group; if <= 0, bits are not grouped
      Returns:
      binary string without prefix or suffix