实际上,不管是常用的10进制转2、8、16进制,还是10进制转R进制、R进制转10进制的方法,都被封装在 Integer 对象中
10 进制转 R 进制
R: 进制
N: 10进制数
| 目标进制 |
方法 |
返回值 |
| 2 进制 |
Integer.toBinaryString(N:int) |
字符串 |
| 8 进制 |
Integer.toOctalString(N:int) |
字符串 |
| 16 进制 |
Integer.toHexString(N:int) |
字符串 |
| R 进制 |
Integer.toString(N:int, R:int) |
字符串 |
R 进制转 10 进制
R: 进制
N: R 进制数字符串
1
| Integer.parseInt(N:String, R:int)
|
附:Demo
Code
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class BinaryConversionTest { public static void main(String[] args) { System.out.println("Just Monika"); int dec_num = 18; System.out.println(Integer.toBinaryString(dec_num)); System.out.println(Integer.toOctalString(dec_num)); System.out.println(Integer.toHexString(dec_num)); System.out.println(Integer.toString(dec_num, 3));
String bin_num = "10010"; System.out.println(Integer.parseInt(bin_num, 2)); } }
|
Output
1 2 3 4 5 6
| Just Monika 10010 22 12 200 18
|