public class

Conversion

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

Class Overview

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

Summary

Public Constructors
Conversion()
Public Methods
static int getHexPossibility(String s)
Determine if a string may, must, or cannot be representing a hexadecimal number.
static int stringToInt(String s, int def, int defaultBase)
Convert to int.
static int stringToInt(String s, int def)
Convert to int.
static int stringToInt(String s)
Convert to int.
static Integer stringToIntUnsafe(String s)
static long stringToLong(String s, long def)
Convert to long.
static long stringToLong(String s)
Convert to long.
static long stringToLong(String s, long def, int defaultBase)
Convert to long.
static Long stringToLongUnsafe(String s)
static int toInt(Object o)
Best effort conversion of an object to an integer.
static int toInt(Object o, int def)
Best effort conversion of an object to an integer.
static long toLong(Object o)
Best effort conversion of an object to an integer.
static long toLong(Object o, long def)
Best effort conversion of an object to a long.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Conversion ()

Public Methods

public static int getHexPossibility (String s)

Determine if a string may, must, or cannot be representing a hexadecimal number.

Returns
  • 0=cannot, 1=may, 2=must (be a hex number)

public static int stringToInt (String s, int def, int defaultBase)

Convert to int.

Parameters
s input string
def the default value
Returns
  • the converted value or the default value on error

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

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

public static Integer stringToIntUnsafe (String s)

Throws
NumberFormatException

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

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

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

public static Long stringToLongUnsafe (String s)

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

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

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

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