Class Conversion

java.lang.Object
com.pnfsoftware.jeb.util.encoding.Conversion

public class Conversion extends Object
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 Details

    • Conversion

      public Conversion()
  • Method Details

    • toInt

      public static int toInt(Object o)
      Best effort conversion of an object to an integer. If the conversion fails, the value returned is 0.
      Parameters:
      o - a String or an Integer
      Returns:
      the converted int
    • toInt

      public static int toInt(Object o, int def)
      Best effort conversion of an object to an integer.
      Parameters:
      o - a String or an Integer
      def - default value to return if the conversion fails
      Returns:
      the converted int
    • toLong

      public static long toLong(Object o)
      Best effort conversion of an object to an integer. If the conversion fails, the value returned is 0.
      Parameters:
      o - a String or an Long
      Returns:
      the converted long
    • toLong

      public static long toLong(Object o, long def)
      Best effort conversion of an object to a long.
      Parameters:
      o - a String or an Long
      def - default value to return if the conversion fails
      Returns:
      the converted long
    • stringToLongUnsafe

      public static Long stringToLongUnsafe(String s)
    • stringToLong

      public static long stringToLong(String s, long def, int defaultBase)
      Convert to long.
      Parameters:
      s - the input string
      def - the default value if the conversion failed
      defaultBase - the default base if the base representation could not be determined
      Returns:
      the converted value or the default value on error
    • stringToLong

      public static long stringToLong(String s, long def)
      Convert to long. The default conversion base, if undetermined, will be 10.
      Parameters:
      s - input string
      def - value to be returned on error
      Returns:
      the converted value
    • stringToLong

      public static long stringToLong(String s)
      Convert to long. The default conversion base, if undetermined, will be 10.
      Parameters:
      s - input string
      Returns:
      the converted value, 0 on error
    • stringToIntUnsafe

      public static Integer stringToIntUnsafe(String s) throws NumberFormatException
      Throws:
      NumberFormatException
    • stringToInt

      public static int stringToInt(String s, int def, int defaultBase)
      Convert to int.
      Parameters:
      s - input string
      def - the default value
      defaultBase -
      Returns:
      the converted value or the default value on error
    • stringToInt

      public static int stringToInt(String s, int def)
      Convert to int. The default conversion base, if undetermined, will be 10.
      Parameters:
      s - input string
      def - value to be returned on error
      Returns:
      the converted value
    • stringToInt

      public static int stringToInt(String s)
      Convert to int. The default conversion base, if undetermined, will be 10.
      Parameters:
      s - input string
      Returns:
      the converted value, 0 on error
    • getHexPossibility

      public static int getHexPossibility(String s)
      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)