Converting Integer Values to Strings in Different Notations – Selected API Classes

Converting Integer Values to Strings in Different Notations

The wrapper classes Integer and Long provide static methods for converting integers to text representations in decimal, binary, octal, and hexadecimal notation. Some of these methods from the Integer class are listed here, but analogous methods are also defined in the Long class. Example 8.2 demonstrates the use of these methods.

Click here to view code image

static String toBinaryString(int i)
static String toHexString(int i)
static String toOctalString(int i)

These three methods return a text representation of the integer argument as an unsigned integer in base 2, 16, and 8, respectively, with no extra leading zeroes.

Click here to view code image

static String toString(int i, int base)
static String toString(int i)

The first method returns the minus sign (-) as the first character if the integer i is negative. In all cases, it returns the text representation of the magnitude of the integer i in the specified base.

The second method is equivalent to the method toString(int i, int base), where the base has the value 10, and which returns the text representation as a signed decimal ((6a) in Figure 8.2).

Example 8.2 Text Representation of Integers

Click here to view code image

public class IntegerRepresentation {
  public static void main(String[] args) {
    int positiveInt = +41;    // 0b101001, 051, 0x29
    int negativeInt = -41;    // 0b11111111111111111111111111010111, -0b101001,
                              // 037777777727, -051, 0xffffffd7, -0x29
    System.out.println(“Text representation for decimal value: ” + positiveInt);
    integerStringRepresentation(positiveInt);
    System.out.println(“Text representation for decimal value: ” + negativeInt);
    integerStringRepresentation(negativeInt);
  }
  public static void integerStringRepresentation(int i) {
    System.out.println(”    Binary:    ” + Integer.toBinaryString(i));
    System.out.println(”    Octal:     ” + Integer.toOctalString(i));
    System.out.println(”    Hex:       ” + Integer.toHexString(i));
    System.out.println(”    Decimal:   ” + Integer.toString(i));
    System.out.println(”    Using toString(int i, int base) method:”);
    System.out.println(”    Base 2:    ” + Integer.toString(i, 2));
    System.out.println(”    Base 8:    ” + Integer.toString(i, 8));
    System.out.println(”    Base 16:   ” + Integer.toString(i, 16));
    System.out.println(”    Base 10:   ” + Integer.toString(i, 10));
  }
}

Output from the program:

Click here to view code image

Text representation for decimal value: 41
    Binary:    101001
    Octal:     51
    Hex:       29
    Decimal:   41
    Using toString(int i, int base) method:
    Base 2:    101001
    Base 8:    51
    Base 16:   29
    Base 10:   41
Text representation for decimal value: -41
    Binary:    11111111111111111111111111010111
    Octal:     37777777727
    Hex:       ffffffd7
    Decimal:   -41
    Using toString(int i, int base) method:
    Base 2:    -101001
    Base 8:    -51
    Base 16:   -29
    Base 10:   -41


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *