Java中在聲明數字時默認採用的是十進制,能夠在數字前加上符號表示數字採用八進制【前面加0(零)】或者十六進制【前面加上0x(零x)】。spa
Java的整型封裝類Integer和Long提供toString(int i,int radix)靜態方法,能夠將一個任意進制的整數轉換爲其餘進制的整數。code
使用Integer或Long的toBinaryString方法將整數轉換爲二進制。blog
使用Integer或Long的toOctalString方法將整數轉換爲八進制。字符串
使用Integer或Long的toHexString方法將整數轉換爲十六進制。io
使用Integer或Long的toString(int i)方法能夠將其餘進制的整數轉換爲十進制的整數的字符串表示。class
public class SystemConversion { public static void main(String[] args) { int iOct = 0567;//八進制數字的聲明,在前面加上0(零) int iTen = 1000;//十進制數字的聲明 int iHex = 0xAbcd;//十六進制數字的聲明,在前面加上0x(零x),x和abcd不區分大小寫 System.out.println("八進制0567裝換成二進制:Integer.toString(iOct, 2)="+Integer.toString(iOct, 2)); System.out.println("八進制0567裝換成二進制:Integer.toBinaryString(iOct)="+Integer.toBinaryString(iOct)); System.out.println("八進制0567裝換成十進制:Integer.toString(iOct, 10)="+Integer.toString(iOct, 10)); System.out.println("八進制0567裝換成十進制:Integer.toString(iOct)="+Integer.toString(iOct)); System.out.println("八進制0567裝換成十六進制:Integer.toString(iOct, 2)="+Integer.toString(iOct, 16)); System.out.println("八進制0567裝換成十六進制:Integer.toHexString(iOct)="+Integer.toHexString(iOct)); System.out.println(); System.out.println("十進制1000裝換成十六進制:Integer.toString(iTen,16)="+Integer.toString(iTen,16)); System.out.println("十進制1000裝換成十六進制:Integer.toHexString(iTen)="+Integer.toHexString(iTen)); System.out.println("十進制1000裝換成八進制:Integer.toString(iTen,8)="+Integer.toString(iTen,8)); System.out.println("十進制1000裝換成八進制:Integer.toOctalString(iTen)="+Integer.toOctalString(iTen)); System.out.println("十進制1000裝換成二進制:Integer.toString(iTen,2)="+Integer.toString(iTen,2)); System.out.println("十進制1000裝換成二進制:Integer.toBinaryString(iTen)="+Integer.toBinaryString(iTen)); System.out.println(); System.out.println("十六進制0xAbcd裝換成十進制:Integer.toString(iHex,10)="+Integer.toString(iHex,10)); System.out.println("十六進制0xAbcd裝換成十進制:Integer.toString(iHex)="+Integer.toString(iHex)); System.out.println("十六進制0xAbcd裝換成八進制:Integer.toString(iHex,8)="+Integer.toString(iHex,8)); System.out.println("十六進制0xAbcd裝換成八進制:Integer.toOctalString(iHex)="+Integer.toOctalString(iHex)); System.out.println("十六進制0xAbcd裝換成二進制:Integer.toString(iHex,2)="+Integer.toString(iHex,2)); System.out.println("十六進制0xAbcd裝換成二進制:Integer.toBinaryString(iHex)="+Integer.toBinaryString(iHex)); System.out.println(); //還可將任意進制的整數裝換成其餘任意進制的數字 System.out.println("十六進制0xAbcd裝換成七進制:Integer.toString(iHex,7)="+Integer.toString(iHex,7)); } } 程序輸出: 八進制0567裝換成二進制:Integer.toString(iOct, 2)=101110111 八進制0567裝換成二進制:Integer.toBinaryString(iOct)=101110111 八進制0567裝換成十進制:Integer.toString(iOct, 10)=375 八進制0567裝換成十進制:Integer.toString(iOct)=375 八進制0567裝換成十六進制:Integer.toString(iOct, 2)=177 八進制0567裝換成十六進制:Integer.toHexString(iOct)=177 十進制1000裝換成十六進制:Integer.toString(iTen,16)=3e8 十進制1000裝換成十六進制:Integer.toHexString(iTen)=3e8 十進制1000裝換成八進制:Integer.toString(iTen,8)=1750 十進制1000裝換成八進制:Integer.toOctalString(iTen)=1750 十進制1000裝換成二進制:Integer.toString(iTen,2)=1111101000 十進制1000裝換成二進制:Integer.toBinaryString(iTen)=1111101000 十六進制0xAbcd裝換成十進制:Integer.toString(iHex,10)=43981 十六進制0xAbcd裝換成十進制:Integer.toString(iHex)=43981 十六進制0xAbcd裝換成八進制:Integer.toString(iHex,8)=125715 十六進制0xAbcd裝換成八進制:Integer.toOctalString(iHex)=125715 十六進制0xAbcd裝換成二進制:Integer.toString(iHex,2)=1010101111001101 十六進制0xAbcd裝換成二進制:Integer.toBinaryString(iHex)=1010101111001101 十六進制0xAbcd裝換成七進制:Integer.toString(iHex,7)=242140