Class Conversions


  • public class Conversions
    extends java.lang.Object
    Functions for converting between strings and numeric values.
    Since:
    2 Sep 2004
    Author:
    Mark Taylor (Starlink)
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int fromHex​(java.lang.String hexVal)
      Converts a string representing a hexadecimal number to its integer value.
      static java.math.BigDecimal parseBigDecimal​(java.lang.String str)
      Attempts to interpret a string as a "BigDecimal" value.
      static java.math.BigInteger parseBigInteger​(java.lang.String str)
      Attempts to interpret a string as a "BigInteger" value.
      static byte parseByte​(java.lang.String str)
      Attempts to interpret a string as a byte (8-bit signed integer) value.
      static double parseDouble​(java.lang.String str)
      Attempts to interpret a string as a double (64-bit signed integer) value.
      static double[] parseDoubles​(java.lang.String str)
      Attempts to interpret a string as an array of floating point values.
      static float parseFloat​(java.lang.String str)
      Attempts to interpret a string as a float (32-bit floating point) value.
      static int parseInt​(java.lang.String str)
      Attempts to interpret a string as an int (32-bit signed integer) value.
      static int[] parseInts​(java.lang.String str)
      Attempts to interpret a string as an array of integer values.
      static long parseLong​(java.lang.String str)
      Attempts to interpret a string as a long (64-bit signed integer) value.
      static short parseShort​(java.lang.String str)
      Attempts to interpret a string as a short (16-bit signed integer) value.
      static byte toByte​(double value)
      Attempts to convert the numeric argument to a byte (8-bit signed integer) result.
      static double toDouble​(double value)
      Converts the numeric argument to a double (64-bit signed integer) result.
      static float toFloat​(double value)
      Attempts to convert the numeric argument to a float (32-bit floating point) result.
      static java.lang.String toHex​(long value)
      Converts the integer argument to hexadecimal form.
      static int toInteger​(double value)
      Attempts to convert the numeric argument to an int (32-bit signed integer) result.
      static long toLong​(double value)
      Attempts to convert the numeric argument to a long (64-bit signed integer) result.
      static short toShort​(double value)
      Attempts to convert the numeric argument to a short (16-bit signed integer) result.
      static java.lang.String toString​(boolean booleanVal)
      Turns a boolean value into a string.
      static java.lang.String toString​(byte byteVal)
      Turns a byte value into a string.
      static java.lang.String toString​(char charVal)
      Turns a single character value into a string.
      static java.lang.String toString​(double fpVal)
      Turns a numeric value into a string.
      static java.lang.String toString​(long intVal)
      Turns an integer numeric value into a string.
      static java.lang.String toString​(java.lang.Object objVal)
      Turns any object value into a string.
      • Methods inherited from class java.lang.Object

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

      • toString

        public static java.lang.String toString​(double fpVal)
        Turns a numeric value into a string.
        Parameters:
        fpVal - floating point numeric value
        Returns:
        a string representation of fpVal
      • toString

        public static java.lang.String toString​(long intVal)
        Turns an integer numeric value into a string.
        Parameters:
        intVal - integer numeric value
        Returns:
        a string representation of intVal
      • toString

        public static java.lang.String toString​(char charVal)
        Turns a single character value into a string.
        Parameters:
        charVal - character numeric value
        Returns:
        a string representation of charVal
      • toString

        public static java.lang.String toString​(byte byteVal)
        Turns a byte value into a string.
        Parameters:
        byteVal - byte numeric value
        Returns:
        a string representation of byteVal
      • toString

        public static java.lang.String toString​(boolean booleanVal)
        Turns a boolean value into a string.
        Parameters:
        booleanVal - boolean value (true or false)
        Returns:
        a string representation of booleanVal ("true" or "false")
      • toString

        public static java.lang.String toString​(java.lang.Object objVal)
        Turns any object value into a string. As applied to existing string values this isn't really useful, but it means that you can apply toString to any object value without knowing its type and get a useful return from it.
        Parameters:
        objVal - non-primitive value
        Returns:
        a string representation of objVal
      • parseByte

        public static byte parseByte​(java.lang.String str)
        Attempts to interpret a string as a byte (8-bit signed integer) value. If the input string can't be interpreted in this way, a blank value will result.
        Parameters:
        str - string containing numeric representation
        Returns:
        byte value of str
      • parseShort

        public static short parseShort​(java.lang.String str)
        Attempts to interpret a string as a short (16-bit signed integer) value. If the input string can't be interpreted in this way, a blank value will result.
        Parameters:
        str - string containing numeric representation
        Returns:
        byte value of str
      • parseInt

        public static int parseInt​(java.lang.String str)
        Attempts to interpret a string as an int (32-bit signed integer) value. If the input string can't be interpreted in this way, a blank value will result.
        Parameters:
        str - string containing numeric representation
        Returns:
        byte value of str
      • parseLong

        public static long parseLong​(java.lang.String str)
        Attempts to interpret a string as a long (64-bit signed integer) value. If the input string can't be interpreted in this way, a blank value will result.
        Parameters:
        str - string containing numeric representation
        Returns:
        byte value of str
      • parseFloat

        public static float parseFloat​(java.lang.String str)
        Attempts to interpret a string as a float (32-bit floating point) value. If the input string can't be interpreted in this way, a blank value will result.
        Parameters:
        str - string containing numeric representation
        Returns:
        byte value of str
      • parseDouble

        public static double parseDouble​(java.lang.String str)
        Attempts to interpret a string as a double (64-bit signed integer) value. If the input string can't be interpreted in this way, a blank value will result.
        Parameters:
        str - string containing numeric representation
        Returns:
        byte value of str
      • parseBigInteger

        public static java.math.BigInteger parseBigInteger​(java.lang.String str)
        Attempts to interpret a string as a "BigInteger" value. This can be used for working with string representations of integers that can't be stored as an unsigned 64-bit value.

        The result is a BigInteger object, which can't be used in normal numeric expressions, but has a number of methods defined on it for comparison, arithmetic, bit manipulation etc. See the java.math.BigInteger javadocs for details.

        Parameters:
        str - string containing numeric representation
        Returns:
        BigInteger value of str
        Examples:
        parseBigInteger("-20000000000000000023").doubleValue() = -2e19, parseBigInteger("18446744073709551616").testBit(64) = true
      • parseBigDecimal

        public static java.math.BigDecimal parseBigDecimal​(java.lang.String str)
        Attempts to interpret a string as a "BigDecimal" value. This can be used for working with string representations of non-integer values that require more precision or range than is possible in a 64-bit IEEE-754 double precision variable.

        The result is a BigDecimal object, which can't be used in normal numeric expressions, but has a number of methods defined on it for comparison, arithmetic, bit manipulation etc. See the java.math.BigDecimal javadocs for details.

        Parameters:
        str - string contining numeric representation
        Returns:
        BigDecimal value of str
        Examples:
        parseBigDecimal("101").compareTo(parseBigDecimal("102")) = -1
      • parseInts

        public static int[] parseInts​(java.lang.String str)
        Attempts to interpret a string as an array of integer values. An ad-hoc algorithm is used that tries to extract a list of integers from a string; a comma- or space-separated list of integer values will work, and other formats may or may not.

        The details of this function's behaviour may change in future releases.

        Parameters:
        str - string containing a list of integer values
        Returns:
        array of integer values
        Examples:
        parseInts("9 8 -23") = [9, 8, -23], parseInts("tiddly-pom") = []
      • parseDoubles

        public static double[] parseDoubles​(java.lang.String str)
        Attempts to interpret a string as an array of floating point values. An ad-hoc algorithm is used that tries to extract a list of numeric values from a string; a comma- or space-separated list of floating point values will work, and other formats may or may not.

        This function can be used as a hacky way to extract the numeric values from an STC-S (for instance ObsCore/EPNcore s_region) string.

        The details of this function's behaviour may change in future releases.

        Parameters:
        str - string containing a list of floating point values
        Returns:
        array of floating point values
        Examples:
        parseDoubles("1.3, 99e1, NaN, -23") = [1.3, 990.0, NaN, -23.0], parseDoubles("Polygon ICRS 0.8 2.1 9.0 2.1 6.2 8.6") = [0.8, 2.1, 9.0, 2.1, 6.2, 8.6], parseDoubles("La la la") = []
      • toByte

        public static byte toByte​(double value)
        Attempts to convert the numeric argument to a byte (8-bit signed integer) result. If it is out of range, a blank value will result.
        Parameters:
        value - numeric value for conversion
        Returns:
        value converted to type byte
      • toShort

        public static short toShort​(double value)
        Attempts to convert the numeric argument to a short (16-bit signed integer) result. If it is out of range, a blank value will result.
        Parameters:
        value - numeric value for conversion
        Returns:
        value converted to type short
      • toInteger

        public static int toInteger​(double value)
        Attempts to convert the numeric argument to an int (32-bit signed integer) result. If it is out of range, a blank value will result.
        Parameters:
        value - numeric value for conversion
        Returns:
        value converted to type int
      • toLong

        public static long toLong​(double value)
        Attempts to convert the numeric argument to a long (64-bit signed integer) result. If it is out of range, a blank value will result.
        Parameters:
        value - numeric value for conversion
        Returns:
        value converted to type long
      • toFloat

        public static float toFloat​(double value)
        Attempts to convert the numeric argument to a float (32-bit floating point) result. If it is out of range, a blank value will result.
        Parameters:
        value - numeric value for conversion
        Returns:
        value converted to type float
      • toDouble

        public static double toDouble​(double value)
        Converts the numeric argument to a double (64-bit signed integer) result.
        Parameters:
        value - numeric value for conversion
        Returns:
        value converted to type double
      • toHex

        public static java.lang.String toHex​(long value)
        Converts the integer argument to hexadecimal form.
        Parameters:
        value - integer value
        Returns:
        hexadecimal representation of value
        Examples:
        toHex(42) = "2a"
      • fromHex

        public static int fromHex​(java.lang.String hexVal)
        Converts a string representing a hexadecimal number to its integer value.
        Parameters:
        hexVal - hexadecimal representation of value
        Returns:
        integer value represented by hexVal
        Examples:
        fromHex("2a") = 42