Class Conversion
java.lang.Object
com.pnfsoftware.jeb.util.encoding.Conversion
Utility methods for safe conversions of strings or boxed representations of numbers, to numbers.
Base 10, 16 and 8 are supported. Numbers in bases 16 and 8 require explicit prefix or suffix.
Otherwise, it is assumed that the string represents a number in base 10. The exceptions are:
- Strings starting with "0x" or ending with "h": assume base 16
- Strings starting with "0?" (? denoting any character): assume base 8
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
Determine if a string may, must, or cannot be representing a hexadecimal number.static int
Convert to int.static int
stringToInt
(String s, int def) Convert to int.static int
stringToInt
(String s, int def, int defaultBase) Convert to int.static Integer
static long
Convert to long.static long
stringToLong
(String s, long def) Convert to long.static long
stringToLong
(String s, long def, int defaultBase) Convert to long.static Long
static int
Best effort conversion of an object to an integer.static int
Best effort conversion of an object to an integer.static long
Best effort conversion of an object to an integer.static long
Best effort conversion of an object to a long.
-
Constructor Details
-
Conversion
public Conversion()
-
-
Method Details
-
toInt
Best effort conversion of an object to an integer. If the conversion fails, the value returned is 0. -
toInt
Best effort conversion of an object to an integer. -
toLong
Best effort conversion of an object to an integer. If the conversion fails, the value returned is 0. -
toLong
Best effort conversion of an object to a long. -
stringToLongUnsafe
-
stringToLong
Convert to long.- Parameters:
s
- the input stringdef
- the default value if the conversion faileddefaultBase
- the default base if the base representation could not be determined- Returns:
- the converted value or the default value on error
-
stringToLong
Convert to long. The default conversion base, if undetermined, will be 10.- Parameters:
s
- input stringdef
- value to be returned on error- Returns:
- the converted value
-
stringToLong
Convert to long. The default conversion base, if undetermined, will be 10.- Parameters:
s
- input string- Returns:
- the converted value, 0 on error
-
stringToIntUnsafe
- Throws:
NumberFormatException
-
stringToInt
Convert to int.- Parameters:
s
- input stringdef
- the default valuedefaultBase
-- Returns:
- the converted value or the default value on error
-
stringToInt
Convert to int. The default conversion base, if undetermined, will be 10.- Parameters:
s
- input stringdef
- value to be returned on error- Returns:
- the converted value
-
stringToInt
Convert to int. The default conversion base, if undetermined, will be 10.- Parameters:
s
- input string- Returns:
- the converted value, 0 on error
-
getHexPossibility
Determine if a string may, must, or cannot be representing a hexadecimal number.- Parameters:
sa
- prefix-less, suffix-less string representation of a number- Returns:
- 0=cannot, 1=may, 2=must (be a hex number)
-