Tuesday, March 6, 2012

How do I convert decimal to octal in Java?


public class IntegerToOctalExample {    
    public static void main(String[] args) {
        int integer = 1024;
        //
        // Converting integer value to octal string representation.
        //
        String octal = Integer.toOctalString(integer);
        
        //
        // Output the value to console.
        //
        System.out.printf("Octal value of %d is '%s'.\n", integer, octal);

        //
        // When for formatting purposes we can actually use printf
        // or String.format to display an integer value in other
        // format (o = octal, h = hexadecimal).
        //
        System.out.printf("Octal value of %1$d is '%1$o'.\n", integer);

        //
        // Now we converting back from octal string to integer
        // by calling Integer.parseInt and passing 8 as the radix.
        //
        int original = Integer.parseInt(octal, 8);
        System.out.printf("Integer value of octal '%s' is %d.", octal, original);
    }
}


Below is output...

Octal value of 1024 is '2000'.
Octal value of 1024 is '2000'.
Integer value of octal '2000' is 1024.

No comments:

Post a Comment