麻烦各位帮忙做一下这个题:
    用移位的方式打印出一个十进制整数的十六进制形式。提示:按每4个二进制位对整数进行移位和去高位处理,得到的结果就是十六进制数的一位,然后按下面三种方式计算出一个十六进制数值对应的十六进制形式: 
•(1)0-9之间的数值直接加上字符'0',9以上的数值减去10以后再加上字符'A' 
•(2)定义一个数组,其中包含0-F这些字符,然后用要计算的数值作为数组的索引号,即可获得其对应的十六进制数据。
•(3)Character.forDigit静态方法可以将一个十六进制的数字转变成其对应的字符表示形式,例如,根据数值15返回字符'F'。

解决方案 »

  1.   

    作业帖的话只贴代码,不负责解释和测试:public class ByteUtil {    private final static char[] HEX = "0123456789abcdef".toCharArray();    private ByteUtil() { }    public static String bytes2StrSpace(byte[] bys) {
            char[] chs = new char[bys.length * 3 - 1];
            for(int i = 0, k = 0; i < bys.length; i++) {
                if(k > 0) {
                    chs[k++] = ' ';
                }
                chs[k++] = HEX[(bys[i] >> 4) & 0xf];
                chs[k++] = HEX[bys[i] & 0xf];
            }
            return new String(chs);
        }
        
        public static String bytes2Str(byte[] bys) {
            char[] chs = new char[bys.length * 2];
            for(int i = 0, k = 0; i < bys.length; i++) {
                chs[k++] = HEX[(bys[i] >> 4) & 0xf];
                chs[k++] = HEX[bys[i] & 0xf];
            }
            return new String(chs);
        }    public static String byte2Str(byte b) {
            char[] chs = new char[2];
            chs[0] = HEX[(b >> 4) & 0xf];
            chs[1] = HEX[b & 0xf];
            return new String(chs);
        }
    }
      

  2.   

    不解释,有出处
    public class Test {
    final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
    '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
    'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
    'z' }; private static String toHexString(int i) {
    char[] buf = new char[32];
    int charPos = 32;
    int radix = 1 << 4;
    int mask = radix - 1;
    do {
    buf[--charPos] = digits[i & mask];
    i >>>= 4;
    } while (i != 0); return new String(buf, charPos, (32 - charPos));
    } public static void main(String[] args) {
    int i = 55;
    System.out.println(toHexString(i));
    }
    }
      

  3.   

    LS的代码出处是JDK中的Integer类,很精练。多看看JDK源码很有好处。