A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小灬清新丶 中级黑马   /  2015-7-11 17:27  /  514 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class ShuZuTest6
{
        public static void main(String[] args)
        {
                //toHex(60);
                toBin(-6);
        }
        public static void toBin(int num)
        {
                //定义二进制的表
                char[] chs = {'0','1'};
                //定义一个临时存储器。
                char[] arr = new char[32];
                //定义一个操作数组指针。
                int pos = arr.length;

                while(num!=0)
                {
                        int temp = num & 1;
                        arr[--pos] = chs[temp];
                        num = num >>>1;

                }
                for (int x=pos;x<arr.length;x++ )
                {
                 System.out.print(arr[x]);
                }
        }

        public static void toHex(int num)
        {
                char[] chs = {'0','1','2','3'
                                        ,'4','5','6','7'
                                        ,'8','9','A','B'
                                        ,'C','D','E','F'};
        //for (int x=0;x<8 ;x++ )
        //定义一个临时容器
        char[] arr = new char[8];
        int pos = arr.length;//指针
       
        while (num!= 0)
                {
                        int temp = num &15;
                        //System.out.println(chs[temp]);
                        arr[--pos] = chs[temp];
                        num = num >>>4;
       
                }
                System.out.println("pos="+pos);
                //遍历arr数组
                for (int x=pos;x<arr.length ;x++ )
                {
                System.out.print(arr[x]+",");
               
                }
               
        }

}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马