The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.lang  [58 examples] > Numbers  [2 examples]

e83. Parsing and Formatting a Number into Binary, Octal, and Hexadecimal

    int i = 1023;
    
    // Parse and format to binary
    i = Integer.parseInt("1111111111", 2); // 1023
    String s = Integer.toString(i, 2);     // 1111111111
    
    // Parse and format to octal
    i = Integer.parseInt("1777", 8);       // 1023
    s = Integer.toString(i, 8);            // 1777
    
    // Parse and format to decimal
    i = Integer.parseInt("1023");          // 1023
    s = Integer.toString(i);               // 1023
    
    // Parse and format to hexadecimal
    i = Integer.parseInt("3ff", 16);       // 1023
    s = Integer.toString(i, 16);           // 3ff
    
    // Parse and format to arbitrary radix <= Character.MAX_RADIX
    int radix = 32;
    i = Integer.parseInt("vv", radix);     // 1023
    s = Integer.toString(i, radix);        // vv

 Related Examples
e82. Converting a String to a Number

See also: Arrays    Assertions    Classes    Commands    Objects    Strings    System Properties    Threads   


© 2002 Addison-Wesley.