Package biz.onixs.util
Class Converter
- java.lang.Object
-
- biz.onixs.util.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 intdouble2str(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 intdouble2str(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 Stringdouble2str(double value)Converts a double value to its string representation with default precision.static Stringdouble2str(double value, int precision)Converts a double value to its string representation with the specified precision.static inti2str(byte[] buffer, int value)Converts an integer value to a string representation and stores it in the provided byte array.static inti2str(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 Stringi2str(int value)Converts an integer to its string representation.static intlong2str(byte[] buffer, int offset, long value)Converts a long value to its string representation and writes it into the provided byte array buffer.static intlong2str(byte[] buffer, long value)Converts a long value into a string representation and writes it into the specified byte array buffer.static voidlong2str(DynamicByteBuffer buffer, long value)Converts a long value to its string representation and writes the result into the specifiedDynamicByteBuffer.static doublestr2double(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 doublestr2double(String s, boolean strict)Converts a given string to a double value.static intstr2int(byte[] buffer, int offset, int length, boolean strict)Converts a sequence of bytes within a buffer to an integer value.static intstr2int(String s, boolean strict)Converts a string representation of a number to its integer value.static longstr2long(byte[] buffer, int offset, int length, boolean strict)Converts a portion of a byte array into a long value.static longstr2long(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 convertedprecision- 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 writtenvalue- the double value to convertprecision- the number of digits after the decimal point in the resulting string- Returns:
- the length in bytes of the converted string
- Throws:
NullPointerException- ifbuffer == nullIllegalArgumentException- ifprecision < 0orprecision > 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 nullstrict- 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 writtenoffset- the position in the byte array where the string representation should startvalue- 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 writtenvalue- 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 nulloffset- the starting position in the buffer where the string representation will begin writing; must be non-negative and less than the length of the buffervalue- 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 nullIllegalArgumentException- 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 specifiedDynamicByteBuffer.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- theDynamicByteBufferwhere the string representation of the long value will be writtenvalue- the long value to be converted to a string representation- Throws:
NullPointerException- if the providedbufferisnull
-
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 integervalue- 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 representationoffset- the starting position in the buffer to read fromlength- the number of bytes to read from the bufferstrict- 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 nullstrict- 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 convertoffset- the starting position in the arraylength- the number of characters to read from the arraystrict- 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 nullstrict- 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
-
-