Java 16进制和int互转

Java 16进制和int互转

/**
 * Java 16进制和int互转
 * 
 * @author www.only-demo.com
 *
 */
class IntHexConverterDemo {
     
    public static void main(String[] args) {
        //1.无符号整型,
        //int转16进制,使用Integer.toHexString(i)转换
        int int100 = 100;
        System.out.println(Integer.toHexString(int100));//64
        int int100_ = -100;
        System.out.println(Integer.toHexString(int100_));//ffffff9c
        //16进制转int,使用Integer.parseUnsignedInt(s, 16)
        System.out.println(Integer.parseUnsignedInt("64", 16));
        System.out.println(Integer.parseUnsignedInt("ffffff9c", 16));
         
        //2.有符号整型
        //int转16进制,使用Integer.toString(i, 16)转换
        System.out.println(Integer.toString(int100, 16));//64
        System.out.println(Integer.toString(int100_, 16));//-64
        //16进制转int,使用Integer.parseInt(s, 16)
        System.out.println(Integer.parseInt("64", 16));//100
        System.out.println(Integer.parseInt("-64", 16));//-100
    }
}

结果

64
ffffff9c
100
-100
64
-64
100
-100

参考:
http://www.only-demo.com/java/20190223/293.html

你可能感兴趣的:(Java 16进制和int互转)