十进制转十六进制

来源:互联网 发布:高淇java300集 知乎 编辑:程序博客网 时间:2024/06/01 09:34

十进制转十六进制中,出现一个小错误


class DetoHex{public static void main(String[] args){toHex(60);}public static void toHex(int num){for(int x=0;x<8;x++){int temp = num & 15;if(temp>9)System.out.println((char)(temp-10+'A'));elseSystem.out.println(temp);int num >>> 4;//这里编译错误,应该是num = num >>> 4}}}

用StringBuffer这个字符容器可以临时装下各个数位的字符,修改后代码:

class DetoHex{public static void main(String[] args){toHex(60);}public static void toHex(int num){StringBuffer sb = new StringBuffer();for(int x=0;x<8;x++){int temp = num & 15;if(temp>9)sb.append((char)(temp-10+'A'));elsesb.append(temp);num = num >>> 4;}System.out.println(sb.reverse());}}


0 0