10进制转16进制

https://www.cnblogs.com/zuofei123/p/5519458.html

    public static void main(String[] args) throws Exception {
        int i = 17;

        //10进制转16进制
        int ii  = Integer.parseInt(Integer.toHexString(i));
        System.out.println(ii);//11

        //16进制转10进制
        ii=Integer.parseInt(Integer.toString(ii),16);
        System.out.println(ii);//17

        //改写10进制转16进制函数
        System.out.println(decimalToHex(18));//12




}
    //改写10进制转16进制函数
    public static String decimalToHex(int decimal) {
        String hex = "";
        int hexValue;
        while(decimal != 0) {
            hexValue = decimal % 16;
            hex = toHexChar(hexValue) + hex;
            decimal = decimal / 16;
        }
        return  hex;
    }

    //将0~15的十进制数转换成0~F的十六进制数
    public static char toHexChar(int hexValue) {
        if(hexValue <= 9 && hexValue >= 0)
            return (char)(hexValue + '0');
        else
            return (char)(hexValue - 10 + 'A');
    } 

猜你喜欢

转载自blog.csdn.net/junjunba2689/article/details/82055756