Class RadixUtils


  • @NotThreadSafe
    public class RadixUtils
    extends java.lang.Object
    The class contains several methods used for work with numbers in various radixes.

    Numbers represented in various radixes are used widely in system programming in all times. This class tries to make parsing, converting and working with various number radixes easier.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  RadixUtils.NumberPattern
      This class represents a number pattern in single radix
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addNumberPattern​(RadixUtils.NumberPattern pattern)
      Add NumberPattern for new automatic radix recognition
      static byte[] convertToNumber​(java.lang.String number, int fromRadix)
      Convert an integer number in any radix (stored in String) to binary components (bytes) in little endian.
      static byte[] convertToNumber​(java.lang.String number, int fromRadix, int bytesCount)
      Convert an integer number in any radix (stored in String) to binary components (bytes) in little endian.
      static java.lang.String convertToRadix​(byte[] number, int toRadix, boolean littleEndian)
      Converts number in any length to a number with specified radix.
      java.lang.String convertToRadix​(java.lang.String number, int toRadix)
      Converts number in any length to a number with specified radix.
      static java.lang.String convertToRadix​(java.lang.String number, int fromRadix, int toRadix)
      Converts number in any length to a number with specified radix.
      static java.lang.String formatBinaryString​(int number, int length)
      Get formatted binary string of given number.
      static java.lang.String formatBinaryString​(int number, int length, int spacePerBits, boolean spacesFromLeft)
      Get formatted binary string of given number.
      static java.lang.String formatByteHexString​(int byteNumber)
      Get formatted string of a byte.
      static java.lang.String formatDwordHexString​(int number)
      Get formatted string of a dword.
      static java.lang.String formatWordHexString​(int wordNumber)
      Get formatted string of a word.
      static java.lang.String formatWordHexString​(short upper, short lower)
      Get formatted string of a word.
      static RadixUtils getInstance()  
      int parseRadix​(java.lang.String number)
      Parses a number in known radix into integer.
      int parseRadix​(java.lang.String number, int radix)
      Parses a number in known radix into integer.
      void setDefaults()
      Clears all user-defined patterns
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getInstance

        public static RadixUtils getInstance()
      • setDefaults

        public void setDefaults()
        Clears all user-defined patterns
      • addNumberPattern

        public void addNumberPattern​(RadixUtils.NumberPattern pattern)
        Add NumberPattern for new automatic radix recognition
        Parameters:
        pattern - NumberPattern instance
      • convertToRadix

        public static java.lang.String convertToRadix​(byte[] number,
                                                      int toRadix,
                                                      boolean littleEndian)
        Converts number in any length to a number with specified radix.
        Parameters:
        number - any-length number. Array of number components stored in little endian.
        toRadix - the radix of converted number
        littleEndian - If the number is in little endian (true), or big endian (false)
        Returns:
        String of a number in specified radix
      • convertToRadix

        public java.lang.String convertToRadix​(java.lang.String number,
                                               int toRadix)
        Converts number in any length to a number with specified radix.

        This method will automatically detect the number original radix. It can detect several hexadecimal, decimal, and octal formats.

        Parameters:
        number - String representing number in hexa, octal or decadic radix
        toRadix - target radix of the number
        Returns:
        String of a number in specified radix
        Throws:
        java.lang.NumberFormatException - if the number is not in known format
      • convertToRadix

        public static java.lang.String convertToRadix​(java.lang.String number,
                                                      int fromRadix,
                                                      int toRadix)
        Converts number in any length to a number with specified radix.
        Parameters:
        number - String representing number in any radix
        fromRadix - source radix of the number
        toRadix - target radix of the number
        Returns:
        String of a number in target radix
      • convertToNumber

        public static byte[] convertToNumber​(java.lang.String number,
                                             int fromRadix)
        Convert an integer number in any radix (stored in String) to binary components (bytes) in little endian.

        Complexity: O(n)

        Parameters:
        number - number stored as String
        fromRadix - the radix of the number
        Returns:
        Array of binary components of that number
      • convertToNumber

        public static byte[] convertToNumber​(java.lang.String number,
                                             int fromRadix,
                                             int bytesCount)
        Convert an integer number in any radix (stored in String) to binary components (bytes) in little endian.

        Complexity: O(n)

        Parameters:
        number - number stored as String
        fromRadix - the radix of the number
        bytesCount - number of bytes. If the results has fewer bytes, they will be appended from the left. If it contains more bytes, they will be cut from the left (from MSB).
        Returns:
        Array of binary components of that number
      • parseRadix

        public int parseRadix​(java.lang.String number)
                       throws java.lang.NumberFormatException
        Parses a number in known radix into integer.
        Parameters:
        number - number in some known radix
        Returns:
        parsed integer
        Throws:
        java.lang.NumberFormatException - if the number is not in known format
      • parseRadix

        public int parseRadix​(java.lang.String number,
                              int radix)
                       throws java.lang.NumberFormatException
        Parses a number in known radix into integer.
        Parameters:
        number - number in some known radix
        radix - radix of the number (known pattern must exist for parsing)
        Returns:
        parsed integer
        Throws:
        java.lang.NumberFormatException - if there is no pattern available for given radix or the number is unparseable
      • formatByteHexString

        public static java.lang.String formatByteHexString​(int byteNumber)
        Get formatted string of a byte.

        Formatting is using pattern "%02X".

        Parameters:
        byteNumber - a number, assumed size is a byte
        Returns:
        formatted string as a hexadecimal number, with string length=2
      • formatWordHexString

        public static java.lang.String formatWordHexString​(int wordNumber)
        Get formatted string of a word.

        Formatting is using pattern "%04X".

        Parameters:
        wordNumber - a number, assumed size is a word (2 bytes)
        Returns:
        formatted string as a hexadecimal number, with string length=4
      • formatWordHexString

        public static java.lang.String formatWordHexString​(short upper,
                                                           short lower)
        Get formatted string of a word.

        Formatting is using pattern "%04X".

        Parameters:
        upper - high order byte (high 8 bits)
        lower - low order byte (low 8bits)
        Returns:
        formatted string as a hexadecimal number, with string length=4
      • formatDwordHexString

        public static java.lang.String formatDwordHexString​(int number)
        Get formatted string of a dword.

        Formatting is using pattern "%08X".

        Parameters:
        number - a number, assumed size is a double word (4 bytes)
        Returns:
        formatted string as a hexadecimal number, with string length=8
      • formatBinaryString

        public static java.lang.String formatBinaryString​(int number,
                                                          int length,
                                                          int spacePerBits,
                                                          boolean spacesFromLeft)
        Get formatted binary string of given number.

        The formatted string is possibly prepended with zeroes to ensure that the string has given length.

        Also, groups of some bits can be separated by single space. The number of space-separated bits is specified by the `spacesPerBits` parameter.

        Parameters:
        number - number to format
        length - resulting string length (number of bits)
        spacePerBits - number of space-separated bits. If <= 0 then bits are never separated with space.
        spacesFromLeft - whether the group of bits to be space-separated should be counted from left or from right side
        Returns:
        formatted string as a binary number, with given string length
      • formatBinaryString

        public static java.lang.String formatBinaryString​(int number,
                                                          int length)
        Get formatted binary string of given number.

        The formatted string is possibly prepended with zeroes to ensure that the string has given length.

        Bits are not separated by spaces.

        Parameters:
        number - number to format
        length - resulting string length (number of bits)
        Returns:
        formatted string as a binary number, with given string length