Java 进制间的转换

package com.touch.onlinedu;

public class Test {

    public static void main(String[] args) {
        // 1  : 0001
        // 2  : 0010
        // 4  : 0100
        // 8  : 1000
        int a = 1 | 2;
        System.out.println("a:" + a);

        /**
         *  其他进制的字符串转化为10进制数
         */
        Integer b = Integer.valueOf("01111111111111111111111111111111", 2);
        System.out.println("b:" + b);

        /**
         *  10进制数转化为其他进制的字符串
         */
        String o = Integer.toString(26, 8);
        System.out.println("o:" + o);

        String o2 = Integer.toOctalString(26);
        System.out.println("o2:" + o2);

        /**
         *  10进制数转化为常用的其他进制(2,8,16)的字符串
         */
        String c = Integer.toBinaryString(-26);
        System.out.println("c:" + c);

        String d = Integer.toBinaryString(26);
        System.out.println("d:" + d);

        String h = Integer.toHexString(26);
        System.out.println("h:" + h);

        /**
         * 进制间的转换可以通过10进制作为桥梁
         * 例如 2进制的101010 转为16禁止是多少呢?
         */
        Integer integer = Integer.valueOf("101010", 2);
        String hex = Integer.toHexString(integer);
        System.out.println("integer:" + integer);
        System.out.println("hex:" + hex);

    }
}
a:3
b:2147483647
o:32
o2:32
c:11111111111111111111111111100110
d:11010
h:1a
integer:42
hex:2a

猜你喜欢

转载自www.cnblogs.com/lanqie/p/9077344.html