Class Converter


  • public class Converter
    extends Object
    A utility class that provides methods to convert between numerical values (double, int, and long) and their string or byte array representations, as well as methods to parse these representations back into numerical values.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int double2str​(byte[] buffer, double value, int precision)
      Converts a double value to its string representation with the specified precision and places it into a byte buffer at the default offset of 0.
      static int double2str​(byte[] buffer, int offset, double value, int precision)
      Converts a double value to its string representation with a specified precision and writes it to the provided byte buffer starting at the specified offset.
      static String double2str​(double value)
      Converts a double value to its string representation with default precision.
      static String double2str​(double value, int precision)
      Converts a double value to its string representation with the specified precision.
      static int i2str​(byte[] buffer, int value)
      Converts an integer value to a string representation and stores it in the provided byte array.
      static int i2str​(byte[] buffer, int offset, int value)
      Converts an integer value to its string representation and writes it to a byte buffer starting at the specified offset.
      static String i2str​(int value)
      Converts an integer to its string representation.
      static int long2str​(byte[] buffer, int offset, long value)
      Converts a long value to its string representation and writes it into the provided byte array buffer.
      static int long2str​(byte[] buffer, long value)
      Converts a long value into a string representation and writes it into the specified byte array buffer.
      static void long2str​(DynamicByteBuffer buffer, long value)
      Converts a long value to its string representation and writes the result into the specified DynamicByteBuffer.
      static double str2double​(byte[] buffer, int offset, int length, boolean strict)
      Converts a sequence of bytes in the provided buffer into a double-precision floating-point number, starting at the specified offset and using the specified length.
      static double str2double​(String s, boolean strict)
      Converts a given string to a double value.
      static int str2int​(byte[] buffer, int offset, int length, boolean strict)
      Converts a sequence of bytes within a buffer to an integer value.
      static int str2int​(String s, boolean strict)
      Converts a string representation of a number to its integer value.
      static long str2long​(byte[] buffer, int offset, int length, boolean strict)
      Converts a portion of a byte array into a long value.
      static long str2long​(String s, boolean strict)
      Converts a string to a long value.
    • Method Detail

      • double2str

        public static String double2str​(double value)
        Converts a double value to its string representation with default precision.
        Parameters:
        value - the double value to convert
        Returns:
        the string representation of the given double value with a default precision of 6
      • double2str

        public static String double2str​(double value,
                                        int precision)
        Converts a double value to its string representation with the specified precision.
        Parameters:
        value - the double value to be converted
        precision - the number of digits after the decimal point in the resulting string
        Returns:
        the string representation of the given double value with the specified precision
      • double2str

        public static int double2str​(byte[] buffer,
                                     double value,
                                     int precision)
        Converts a double value to its string representation with the specified precision and places it into a byte buffer at the default offset of 0.
        Parameters:
        buffer - the target buffer where the string representation of the double value will be written
        value - the double value to convert
        precision - the number of digits after the decimal point in the resulting string
        Returns:
        the length in bytes of the converted string
        Throws:
        NullPointerException - if buffer == null
        IllegalArgumentException - if precision < 0 or precision > 10
      • double2str

        public static int double2str​(byte[] buffer,
                                     int offset,
                                     double value,
                                     int precision)
        Converts a double value to its string representation with a specified precision and writes it to the provided byte buffer starting at the specified offset.
        Parameters:
        buffer - The byte array where the string representation of the double value will be written. Must not be null and must have sufficient space for the resulting string.
        offset - The starting position in the buffer where the string representation will be written. Must be within the bounds of the buffer.
        value - The double value to convert to a string representation.
        precision - The number of digits to include after the decimal point. Must be between 0 and 10 inclusive.
        Returns:
        The number of bytes written to the buffer.
        Throws:
        NullPointerException - If the buffer is null.
        IllegalArgumentException - If the offset is out of bounds of the buffer or if the precision is outside the allowed range (0-10).
      • str2double

        public static double str2double​(byte[] buffer,
                                        int offset,
                                        int length,
                                        boolean strict)
        Converts a sequence of bytes in the provided buffer into a double-precision floating-point number, starting at the specified offset and using the specified length. The method can optionally enforce strict parsing rules.
        Parameters:
        buffer - the byte array containing the sequence of characters to convert.
        offset - the starting position within the buffer where the number begins.
        length - the number of bytes to parse from the buffer.
        strict - if true, enforces strict parsing, ensuring that only valid number formats and trailing spaces are allowed.
        Returns:
        the converted double value.
        Throws:
        NullPointerException - if the input buffer is null.
        IllegalArgumentException - if the offset is negative, the length is negative, or if the offset and length exceed the buffer bounds.
        NumberFormatException - if the input does not contain a valid numeric value.
      • str2double

        public static double str2double​(String s,
                                        boolean strict)
        Converts a given string to a double value. The conversion can optionally enforce strict parsing rules.
        Parameters:
        s - the string to be converted to a double; must not be null
        strict - whether to enforce strict parsing rules
        Returns:
        the double value parsed from the string
        Throws:
        NullPointerException - if the input string is null
      • i2str

        public static int i2str​(byte[] buffer,
                                int offset,
                                int value)
        Converts an integer value to its string representation and writes it to a byte buffer starting at the specified offset.
        Parameters:
        buffer - the byte array to which the string representation of the integer will be written
        offset - the position in the byte array where the string representation should start
        value - the integer value to be converted to a string representation
        Returns:
        the number of bytes written to the buffer
      • i2str

        public static String i2str​(int value)
        Converts an integer to its string representation.
        Parameters:
        value - the integer value to convert to a string
        Returns:
        the string representation of the given integer
      • long2str

        public static int long2str​(byte[] buffer,
                                   long value)
        Converts a long value into a string representation and writes it into the specified byte array buffer.
        Parameters:
        buffer - the byte array where the string representation of the long value will be written
        value - the long value to be converted into a string
        Returns:
        the length of the string representation written into the buffer
      • long2str

        public static int long2str​(byte[] buffer,
                                   int offset,
                                   long value)
        Converts a long value to its string representation and writes it into the provided byte array buffer. The conversion starts at a specified offset within the buffer.
        Parameters:
        buffer - the byte array to write the string representation of the long value into; must not be null
        offset - the starting position in the buffer where the string representation will begin writing; must be non-negative and less than the length of the buffer
        value - the long value to be converted to its string representation
        Returns:
        the updated offset in the buffer after writing the string representation
        Throws:
        NullPointerException - if the buffer is null
        IllegalArgumentException - if the offset is negative or greater than or equal to the buffer length
      • long2str

        public static void long2str​(DynamicByteBuffer buffer,
                                    long value)
        Converts a long value to its string representation and writes the result into the specified DynamicByteBuffer.

        The method handles negative values by prefixing the result with a '-' sign. Digits are written in reverse order into the buffer and then reversed back to the correct order.

        Parameters:
        buffer - the DynamicByteBuffer where the string representation of the long value will be written
        value - the long value to be converted to a string representation
        Throws:
        NullPointerException - if the provided buffer is null
      • i2str

        public static int i2str​(byte[] buffer,
                                int value)
        Converts an integer value to a string representation and stores it in the provided byte array. The integer value is encoded starting from the beginning of the buffer.
        Parameters:
        buffer - the byte array to store the string representation of the integer
        value - the integer value to be converted to a string
        Returns:
        the number of bytes written to the buffer representing the string
      • str2int

        public static int str2int​(byte[] buffer,
                                  int offset,
                                  int length,
                                  boolean strict)
        Converts a sequence of bytes within a buffer to an integer value.
        Parameters:
        buffer - the byte array containing the numeric string representation
        offset - the starting position in the buffer to read from
        length - the number of bytes to read from the buffer
        strict - a flag indicating whether to enforce strict parsing rules
        Returns:
        the integer value obtained by parsing the specified sequence of bytes
      • str2int

        public static int str2int​(String s,
                                  boolean strict)
        Converts a string representation of a number to its integer value.
        Parameters:
        s - the string to be converted must not be null
        strict - if true, enforces strict interpretation of the string; if false, allows more lenient handling of the conversion
        Returns:
        the integer value represented by the string
        Throws:
        NullPointerException - if the input string is null
      • str2long

        public static long str2long​(byte[] buffer,
                                    int offset,
                                    int length,
                                    boolean strict)
        Converts a portion of a byte array into a long value.
        Parameters:
        buffer - the byte array containing the characters to convert
        offset - the starting position in the array
        length - the number of characters to read from the array
        strict - a flag indicating whether to enforce strict parsing rules
        Returns:
        the long value resulting from the conversion
      • str2long

        public static long str2long​(String s,
                                    boolean strict)
        Converts a string to a long value.
        Parameters:
        s - the string to be converted; must not be null
        strict - a flag indicating whether strict parsing should be enforced
        Returns:
        the long value obtained by parsing the input string
        Throws:
        NullPointerException - if the provided string is null