首页 > 代码库 > java通过移位转16进制

java通过移位转16进制

public class Main {    public static void main(String []args)    {        Main main = new Main();        System.out.println("main: "+main.Hex(j));        System.out.println("Sys: "+Integer.toHexString(j));    }    private String Hex(int i)    {        String string="";        String temp = Integer.toBinaryString(i);        int len = temp.length();        System.out.println(temp);        System.out.println(len);        while(len%4!=0)        {            temp="0"+temp;            len=temp.length();        }        while(len>0)        {            //System.out.println(len+" "+string);            int bit = 0;            if(temp.charAt(len-1)==‘1‘)            {                bit+=1;            }            if(temp.charAt(len-2)==‘1‘)            {                bit+=2;            }            if(temp.charAt(len-3)==‘1‘)            {                bit+=4;            }            if(temp.charAt(len-4)==‘1‘)            {                bit+=8;            }            switch (bit) {            case 10:                string=‘A‘+string;                break;            case 11:                string=‘B‘+string;                break;            case 12:                string=‘C‘+string;                break;            case 13:                string=‘D‘+string;                break;            case 14:                string=‘E‘+string;                break;            case 15:                string=‘F‘+string;                break;            default:                string=bit+string;                break;            }            len-=4;        }                return string;    }}

 

java通过移位转16进制