各位高手大家好,我是newcomer!!!
有个10进制转换16进制的题目我盖不过来了
public class t1
{
      public static void main(String args[]){
          char c[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
          String str=javax.swing.JOptionPane.showInputDialog("Please input the number for 
changing Decimal to Hex:");//输入框
          byte b = (byte)Integer.parseInt(str);
          System.out.print(c[(b>>4) & 0x0f]);
          System.out.println(c[b & 0x0f]);
     }
}
这个程序是没错的但是只能转换结果为“两位数” 的16进制数(例如输入18888得出的是C8,正确应是49C8),应该是先判断输入数的范围,可是搞了半天也没搞成,请
大哥大姐们细细讲解,谢了!!!

解决方案 »

  1.   

    import java.util.ArrayList;
    import java.util.*;public class Test {
    public static void main(String args[]) {
    char c[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
    'B', 'C', 'D', 'E', 'F' };
    System.out.print("num:");
    Scanner input = new Scanner(System.in);
    int num = input.nextInt();
    ArrayList list = new ArrayList();
    while (num > 0) {
    int k = num % 16;
    list.add(c[k]); num /= 16;
    }
    for (int j = list.size() - 1; j >= 0; j--) {
    System.out.print(list.get(j));
    }
    }
    }
      

  2.   

    byte b = (byte)Integer.parseInt(str); 
    为什么用BYTE?byte最大只能到255。
      

  3.   

    String 16fmt = Integer.valueOf("10fmtNumber").toHexString();
      

  4.   

    1楼的程序运行不出来啊“cannot find symbol"怎么改阿????
      

  5.   

    2楼 我换成 int b = (int)Integer.parseInt(arg[0]);还是不能运行大的数阿 为什么阿