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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© chuanyueing 中级黑马   /  2013-2-21 11:13  /  1273 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public static void toHex(int val) //十进制转十六进制的方法
        {
                int temp;
                for (int x = 0;x <8 ;x++ )
                {
                        temp = val & 15;
                        if (temp > 9)
                                System.out.print(char(temp-10+'A'));
                        else
                                System.out.print(temp);
                        temp = temp >>> 4;
                }
                System.out.println();
        }
不知道哪里错了。。。谁能指点一下..

7 个回复

倒序浏览
System.out.print(char(temp-10+'A'));改成System.out.print((char)(temp-10+'A'));就不报错了
感觉你写错了...
回复 使用道具 举报
System.out.print(char(temp-10+'A'));//(char)(temp-10+'A')
                                                   //还有在注意下要逆序输出
回复 使用道具 举报
李桐 发表于 2013-2-21 11:26
System.out.print(char(temp-10+'A'));改成System.out.print((char)(temp-10+'A'));就不报错了
感觉你写错 ...

还是不对啊,亲
回复 使用道具 举报
朱玺 发表于 2013-2-21 12:07
还是不对啊,亲

郁闷坏了:'(
回复 使用道具 举报
本帖最后由 黑马刘杰 于 2013-2-21 12:41 编辑
  1. public static void toHex(int val) // 十进制转十六进制的方法
  2.         {
  3.                 int temp;
  4.                 for (int x = 0; x < 8; x++) {
  5.                         //这句有什么用?
  6.                         temp = val & 15;
  7.                         if (temp > 9)
  8.                                 System.out.print((char) (temp - 10 + 'A'));
  9.                         else
  10.                                 System.out.print(temp);
  11.                         temp = temp >>> 4;
  12.                 }
  13.                 System.out.println();
  14.         }
复制代码
进制转换你看这样怎么样
  1. /*
  2.          * num:要转换的十进制数值
  3.          * x:进制
  4.          */
  5.         public static void foo(int num,int x){
  6.                 int tmp=0;
  7.                 if (num > 0) {
  8.                             foo(num / x,x);
  9.                             if((tmp=num % x)>=10){
  10.                                     System.out.println((char)(tmp-10+'a'));
  11.                             }else{
  12.                                     System.out.print(tmp);
  13.                             }
  14.                            
  15.                     }
  16.         }
复制代码
回复 使用道具 举报
胥文 中级黑马 2013-2-21 12:45:13
7#
本帖最后由 胥文 于 2013-2-21 12:48 编辑

public class Test19 {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                toHex(100);
        }
        public static void toHex(int value)
        {
               
                int temp;
                char c;
                StringBuilder sb = new StringBuilder();//定义一个字符串缓冲区用来存储字符,以便后面反转
                for(int x =0;x<8;x++)
                {
                        if(value>0)
                        {
                                temp = value & 15;
                                if(temp>9)
                                {        
                                        c=(char)(temp-10+'A');
                                        sb.append(c);
                                        System.out.println(c);
                                }
                                else
                                {        
                                        sb.append(temp);
                                        System.out.println(temp);
                                }
                                value = value>>>4;//注意:此处你写错了,应该是你传的值在变化
                        }
                        else
                                break;
                        //System.out.println(value>>4);
                        
                }
                System.out.println("------十六进制表示为------"+sb.reverse());//这是转换的结果
        }
}
这样就好了
回复 使用道具 举报
胥文 发表于 2013-2-21 12:45
public class Test19 {

        /**

噢啦,这个正确的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马