Class Conversions
java.lang.Object
uk.ac.starlink.ttools.func.Conversions
Functions for converting between strings and numeric values.
- Since:
- 2 Sep 2004
- Author:
- Mark Taylor (Starlink)
-
Method Summary
Modifier and TypeMethodDescriptionstatic intConverts a string representing a hexadecimal number to its integer value.static BigDecimalparseBigDecimal(String str) Attempts to interpret a string as a "BigDecimal" value.static BigIntegerparseBigInteger(String str) Attempts to interpret a string as a "BigInteger" value.static byteAttempts to interpret a string as a byte (8-bit signed integer) value.static doubleparseDouble(String str) Attempts to interpret a string as a double (64-bit signed integer) value.static double[]parseDoubles(String str) Attempts to interpret a string as an array of floating point values.static floatparseFloat(String str) Attempts to interpret a string as a float (32-bit floating point) value.static intAttempts to interpret a string as an int (32-bit signed integer) value.static int[]Attempts to interpret a string as an array of integer values.static longAttempts to interpret a string as a long (64-bit signed integer) value.static shortparseShort(String str) Attempts to interpret a string as a short (16-bit signed integer) value.static bytetoByte(double value) Attempts to convert the numeric argument to a byte (8-bit signed integer) result.static doubletoDouble(double value) Converts the numeric argument to a double (64-bit signed integer) result.static floattoFloat(double value) Attempts to convert the numeric argument to a float (32-bit floating point) result.static StringtoHex(long value) Converts the integer argument to hexadecimal form.static inttoInteger(double value) Attempts to convert the numeric argument to an int (32-bit signed integer) result.static longtoLong(double value) Attempts to convert the numeric argument to a long (64-bit signed integer) result.static shorttoShort(double value) Attempts to convert the numeric argument to a short (16-bit signed integer) result.static StringtoString(boolean booleanVal) Turns a boolean value into a string.static StringtoString(byte byteVal) Turns a byte value into a string.static StringtoString(char charVal) Turns a single character value into a string.static StringtoString(double fpVal) Turns a numeric value into a string.static StringtoString(long intVal) Turns an integer numeric value into a string.static StringTurns any object value into a string.
-
Method Details
-
toString
Turns a numeric value into a string.- Parameters:
fpVal- floating point numeric value- Returns:
- a string representation of
fpVal
-
toString
Turns an integer numeric value into a string.- Parameters:
intVal- integer numeric value- Returns:
- a string representation of
intVal
-
toString
Turns a single character value into a string.- Parameters:
charVal- character numeric value- Returns:
- a string representation of
charVal
-
toString
Turns a byte value into a string.- Parameters:
byteVal- byte numeric value- Returns:
- a string representation of
byteVal
-
toString
Turns a boolean value into a string.- Parameters:
booleanVal- boolean value (true or false)- Returns:
- a string representation of
booleanVal("true" or "false")
-
toString
Turns any object value into a string. As applied to existing string values this isn't really useful, but it means that you can applytoStringto 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
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
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
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
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
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
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
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
BigIntegerobject, 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
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
BigDecimalobject, 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
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
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:
valueconverted 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:
valueconverted 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:
valueconverted 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:
valueconverted 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:
valueconverted 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:
valueconverted to type double
-
toHex
Converts the integer argument to hexadecimal form.- Parameters:
value- integer value- Returns:
- hexadecimal representation of
value - Examples:
toHex(42) = "2a"
-
fromHex
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
-