How do I Convert Decimal to Octal?

Bookmark this example!  
Category: java.lang, viewed: 1540 time(s).
 
package org.kodejava.example.java.lang;
 
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);
    }
}
 
 

Here is the result of our program.

Octal value of 1024 is '2000'.
Octal value of 1024 is '2000'.
Integer value of octal '2000' is 1024.
Can't find what you are looking for? Join our FORUMS and ask some questions!

Most Viewed Examples

Latest Code Examples

100 Top & Latest


Locations of visitors to this page
eXTReMe Tracker
visitor stats
Get Chitika Premium