Java 进制之间互相转换

public static void main(String[] args) {  
    int i = 36;  
    //十进制转  
    String hexString = Integer.toHexString(i); // 16进制  
    String octalString = Integer.toOctalString(i); //8进制  
    String binaryString = Integer.toBinaryString(i); //2进制  
  
    System.out.println("十进制 " + i + " 转成16进制 结果为:" + hexString);  
    System.out.println("十进制 " + i + " 转成8进制 结果为:" + octalString);  
    System.out.println("十进制 " + i + " 转成2进制 结果为:" + binaryString);  
  
    //16进制 转成 10进制  
    String str_num_16 = "FFFF";  
    int num_10_16 = Integer.valueOf(str_num_16,16);  
    System.out.println("16进制 " + str_num_16 + " 转成10进制 结果为:" + num_10_16);  
  
    //8进制 转成 10进制  
    String str_num_8 = "776";  
    int num_10_8 = Integer.valueOf(str_num_8,8);  
    System.out.println("8进制 " + str_num_8 + " 转成10进制 结果为:" + num_10_8);  
  
    //2进制 转成 10进制  
    String str_num_2 = "0101";  
    int num_10_2 = Integer.valueOf(str_num_2,2);  
    System.out.println("2进制 " + str_num_2 + " 转成10进制 结果为:" + num_10_2);  
  
    //16进制 转 2进制  
    String num_2_16 = Integer.toBinaryString(num_10_16);  
    System.out.println("16进制 " + str_num_16 + " 转成2进制 结果为:" + num_2_16);  
     
    //2进制 转 16进制  
    String num_16_2 = Integer.toHexString(num_10_2);  
    System.out.println("2进制 " + str_num_2 + " 转成16进制 结果为:" + num_16_2);  

}

 

 

 Integer factor = 650;

		    String s;

		    s = String.format("%d", factor);	//10
		    System.out.println(s);
		    s = String.format("%x", factor);	//16
		    System.out.println(s);
		    s = String.format("%o", factor);  	//8
		    System.out.println(s);

你可能感兴趣的:(java)