package com.main;public class Main { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(hexToStr(500));
}
public static String hexToStr(int hex){
int i=hex%16;
int j=hex/16;
StringBuffer buf=new StringBuffer("");
if(j>16){
System.out.println(j);
buf.append(i);
hexToStr(j);
}else{
buf.append(i);
buf.append(j);
}
return buf.reverse().toString();
}
}
以上附上我的程序代码,小弟想用程序解决10进制转16进制的问题。可是,我的算法有问题,查了很久不知道怎么解决。帮帮忙  谢谢

解决方案 »

  1.   

    API里面有个方法。。好像是toHexString(参数)
    转换成16进行吧
    自己找一下!
    说错不要介意,偶新手
      

  2.   

    Integer类里的方法toString(int i, int radix) 
    toHexString(int i)
      

  3.   

    public class BitTest { public static void main(String[] args) {
    int k = 5033000;
    System.out.println(toHex(k));
    System.out.println(Integer.toHexString(k));
    System.out.println(toHex(-k));
    System.out.println(Integer.toHexString(-k));
    System.out.println(Integer.toString(-k, 16));
    } public static String toHex(int hex) {
    char[] c = {'0','1','2','3','4','5','6','7',
        '8','9','A','B','C','D','E','F'};
    StringBuffer buf = new StringBuffer();
    boolean isNegative = hex < 0;
    hex = isNegative ? -hex : hex;
    while(hex != 0) {
    int k = hex % 16;
    hex /= 16;
    buf.append(c[k]);
    }
    if(isNegative) {
    buf.append("-");
    }
    return buf.reverse().toString();
    }
    }该方法仅能模拟 Integer.toString(num, 16) 的方法,即负数时直接在前面加上“-”。