net.rim.device.api.util
Class NumberUtilities

java.lang.Object
  |
  +--net.rim.device.api.util.NumberUtilities

public final class NumberUtilities
extends Object

Utility methods for converting numeric values to their character/string digit equivalents.


Method Summary
static void appendNumber(StringBuffer strBuf, byte num, int radix)
          Appends digits for byte value to buffer.
static void appendNumber(StringBuffer strBuf, byte num, int radix, int minWidth)
          Appends digits with padding for byte value to buffer.
static void appendNumber(StringBuffer strBuf, int num)
          Appends digits for decimal integer value to buffer.
static void appendNumber(StringBuffer strBuf, int num, int radix)
          Appends digits for integer value to buffer.
static void appendNumber(StringBuffer strBuf, int num, int radix, int minWidth)
          Appends digits with padding for integer value to buffer.
static void appendNumber(StringBuffer strBuf, long num)
          Appends digits for decimal long integer value to buffer.
static void appendNumber(StringBuffer strBuf, long num, int radix)
          Appends digits for long integer value to buffer.
static void appendNumber(StringBuffer strBuf, long num, int radix, int minWidth)
          Appends digits with padding for long integer value to buffer.
static void appendNumber(StringBuffer strBuf, short num, int radix)
          Appends digits for short integer value to buffer.
static void appendNumber(StringBuffer strBuf, short num, int radix, int minWidth)
          Appends digits with padding for short integer value to buffer.
static int hexDigitToInt(char dig)
          Converts hex digit to integer value.
static int hexDigitToInt(char dig, int err_val)
          Converts hex digit to integer value, using provided error value.
static char intToHexDigit(int value)
          Converts integer value to hex digit.
static char intToUpperHexDigit(int value)
          Converts integer value to upper case hex digit.
static int parseInt(String s, int fromIndex, int toIndex, int radix)
          Parses the string argument as a signed integer in the radix specified by the second argument.
static String toString(byte num, int radix)
          Retrieves digits for byte value.
static String toString(byte num, int radix, int minWidth)
          Retrieves digits with padding for byte value.
static String toString(int num, int radix)
          Retrieves digits for integer value.
static String toString(int num, int radix, int minWidth)
          Retrieves digits with padding for integer value.
static String toString(long num, int radix)
          Retrieves digits for long integer value.
static String toString(long num, int radix, int minWidth)
          Retrieves digits with padding for long integer value.
static String toString(short num, int radix)
          Retrieves digits for short integer value.
static String toString(short num, int radix, int minWidth)
          Retrieves digits with padding for short integer value.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail


appendNumber

public static final void appendNumber(StringBuffer strBuf,
                                      byte num,
                                      int radix)
Appends digits for byte value to buffer.

Parameters:
strBuf - Buffer to contain result.
num - Value to convert.
radix - Radix for value.

appendNumber

public static final void appendNumber(StringBuffer strBuf,
                                      byte num,
                                      int radix,
                                      int minWidth)
Appends digits with padding for byte value to buffer.

Parameters:
strBuf - Buffer to contain result.
num - Value to convert.
radix - Radix for value.
minWidth - Pads digits with leading zeros to meet this width.

appendNumber

public static final void appendNumber(StringBuffer strBuf,
                                      int num)
Appends digits for decimal integer value to buffer.

Parameters:
strBuf - Buffer to contain result.
num - Value to convert.

appendNumber

public static final void appendNumber(StringBuffer strBuf,
                                      int num,
                                      int radix)
Appends digits for integer value to buffer.

Parameters:
strBuf - Buffer to contain result.
num - Value to convert.
radix - Radix for value.

appendNumber

public static final void appendNumber(StringBuffer strBuf,
                                      int num,
                                      int radix,
                                      int minWidth)
Appends digits with padding for integer value to buffer.

Parameters:
strBuf - Buffer to contain result.
num - Value to convert.
radix - Radix for value.
minWidth - Pads digits with leading zeros to meet this width.

appendNumber

public static final void appendNumber(StringBuffer strBuf,
                                      long num)
Appends digits for decimal long integer value to buffer.

Parameters:
strBuf - Buffer to contain result.
num - Value to convert.

appendNumber

public static final void appendNumber(StringBuffer strBuf,
                                      long num,
                                      int radix)
Appends digits for long integer value to buffer.

Parameters:
strBuf - Buffer to contain result.
num - Value to convert.
radix - Radix for value.

appendNumber

public static final void appendNumber(StringBuffer strBuf,
                                      long num,
                                      int radix,
                                      int minWidth)
Appends digits with padding for long integer value to buffer.

Parameters:
strBuf - Buffer to contain result.
num - Value to convert.
radix - Radix for value.
minWidth - Pads digits with leading zeros to meet this width.

appendNumber

public static final void appendNumber(StringBuffer strBuf,
                                      short num,
                                      int radix)
Appends digits for short integer value to buffer.

Parameters:
strBuf - Buffer to contain result.
num - Value to convert.
radix - Radix for value.

appendNumber

public static final void appendNumber(StringBuffer strBuf,
                                      short num,
                                      int radix,
                                      int minWidth)
Appends digits with padding for short integer value to buffer.

Parameters:
strBuf - Buffer to contain result.
num - Value to convert.
radix - Radix for value.
minWidth - Pads digits with leading zeros to meet this width.

hexDigitToInt

public static int hexDigitToInt(char dig)
                         throws NumberFormatException
Converts hex digit to integer value.

Parameters:
dig - Single hex digit (can be lower or upper-case).
Returns:
Integer value for hex digit (value between 0 and 15).
Throws:
NumberFormatException - If provided digit isn't valid hex digit.

hexDigitToInt

public static int hexDigitToInt(char dig,
                                int err_val)
Converts hex digit to integer value, using provided error value.

Parameters:
dig - Single hex digit (can be lower or upper-case).
err_val - Error value to return if provided digit isn't a valid hex digit.
Returns:
Integer value for hex digit (value between 0 and 15).

intToHexDigit

public static char intToHexDigit(int value)
Converts integer value to hex digit.

Parameters:
value - Lowest 4 bits of this value used to create the hex digit.
Returns:
Hex digit for lowest 4 bits of provided value.

intToUpperHexDigit

public static char intToUpperHexDigit(int value)
Converts integer value to upper case hex digit.

Parameters:
value - Lowest 4 bits of this value used to create the upper-case hex digit.
Returns:
Upper-case hex digit for lowest 4 bits of provided value.

parseInt

public static int parseInt(String s,
                           int fromIndex,
                           int toIndex,
                           int radix)
                    throws NumberFormatException
Parses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002d') to indicate a negative value. The resulting integer value is returned.

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  • Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002d') provided that the string is longer than length 1.
  • The integer value represented by the string is not a value of type int.

Examples:

 parseInt("0", 10) returns 0
 parseInt("473", 10) returns 473
 parseInt("-0", 10) returns 0
 parseInt("-FF", 16) returns -255
 parseInt("1100110", 2) returns 102
 parseInt("2147483647", 10) returns 2147483647
 parseInt("-2147483648", 10) returns -2147483648
 parseInt("2147483648", 10) throws a NumberFormatException
 parseInt("99", 8) throws a NumberFormatException
 parseInt("Kona", 10) throws a NumberFormatException
 parseInt("Kona", 27) returns 411787
 
Parameters:
s - the String containing the integer.
fromIndex - the index in the string to start parsing at.
toIndex - the index in the string to end parsing at.
radix - the radix to be used.
Returns:
the integer represented by the string argument in the specified radix.
Throws:
NumberFormatException - if the string does not contain a parsable integer.
Since:
JDE 4.0.0

toString

public static final String toString(byte num,
                                    int radix)
Retrieves digits for byte value.

Parameters:
num - Value to convert.
radix - Radix for value.
Returns:
String containing digits for byte value.

toString

public static final String toString(byte num,
                                    int radix,
                                    int minWidth)
Retrieves digits with padding for byte value.

Parameters:
num - Value to convert.
radix - Radix for value.
minWidth - Pads digits with leading zeros to meet this width.
Returns:
String containing digits for byte value.

toString

public static final String toString(int num,
                                    int radix)
Retrieves digits for integer value.

Parameters:
num - Value to convert.
radix - Radix for value.
Returns:
String containing digits for integer value.

toString

public static final String toString(int num,
                                    int radix,
                                    int minWidth)
Retrieves digits with padding for integer value.

Parameters:
num - Value to convert.
radix - Radix for value.
minWidth - Pads digits with leading zeros to meet this width.
Returns:
String containing digits for integer value.

toString

public static final String toString(long num,
                                    int radix)
Retrieves digits for long integer value.

Parameters:
num - Value to convert.
radix - Radix for value.
Returns:
String containing digits for long integer value.

toString

public static final String toString(long num,
                                    int radix,
                                    int minWidth)
Retrieves digits with padding for long integer value.

Parameters:
num - Value to convert.
radix - Radix for value.
minWidth - Pads digits with leading zeros to meet this width.
Returns:
String containing digits for long integer value.

toString

public static final String toString(short num,
                                    int radix)
Retrieves digits for short integer value.

Parameters:
num - Value to convert.
radix - Radix for value.
Returns:
String containing digits for short integer value.

toString

public static final String toString(short num,
                                    int radix,
                                    int minWidth)
Retrieves digits with padding for short integer value.

Parameters:
num - Value to convert.
radix - Radix for value.
minWidth - Pads digits with leading zeros to meet this width.
Returns:
String containing digits for short integer value.



Copyright 1999-2009 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Copyright 1993-2003 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
Copyright 2002-2003 Nokia Corporation All Rights Reserved.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.