public String encodeHex(int integer) { 
         StringBuffer buf = new StringBuffer(2); 
             if (((int) integer & 0xff) < 0x10) { 
                 buf.append("0"); 
             } 
             buf.append(Long.toString((int) integer & 0xff, 16)); 
         return buf.toString(); 
     }
我现在想实现这个方法的逆功能,即:输入参数是16进制的字符串,返回参数是其中每个16进制字符串的int型数字。
例如:输入字符串“3e5fa8”,最后得到“3e”、“5f”、“a8”共3个int型数字。最后把得到的数字存入int code[]中。
求解答,谢谢。 

解决方案 »

  1.   

    为什么得到一个数组?得到数字不更好吗?Integer.valueOf(“3e5fa8”,16)
      

  2.   

    2楼的朋友。
    因为我希望得到“3e”,“5f”,“a8”分别对应的值,而且输入参数有可能比较长。
      

  3.   

    for example
    int[] hex2Int(String src) {
        byte[] b = src.getBytes();
        int[] result = new int[b.length/2];
        for (int i=0, j=0; i<b.length; i+=2) {
            if (b[i]>=(byte)'0' && b[i]<=(byte)'9') {
                result[j] = (int)(b[i]-(byte)'0');
            } else if (b[i]>=(byte)'a' && b[i]<=(byte)'f') {
                result[j] = 10 + (int)(b[i]-(byte)'a');
            } else if (b[i]>=(byte)'A' && b[i]<=(byte)'F') {
                result[j] = 10 + (int)(b[i]-(byte)'A');
            } else {result [j] = 0;}
            result[j] *= 16;
            if (b[i+1]>=(byte)'0' && b[i+1]<=(byte)'9') {
                result[j] += (int)(b[i+1]-(byte)'0');
            } else if (b[i+1]>=(byte)'a' && b[i+1]<=(byte)'f') {
                result[j] += (10 + (int)(b[i+1]-(byte)'a'));
            } else if (b[i+1]>=(byte)'A' && b[i+1]<=(byte)'F') {
                result[j] += (10 + (int)(b[i+1]-(byte)'A'));
            } else {result[j] += 0;}
            j++;
            if (j==result.length) break;
        }
    }
      

  4.   

    额,我就是这个意思了。初学者不会啊。输入的参数(16进制的String类型字符串)是未知的长度,怎么分割?
      

  5.   

    给个通用的吧,这么简单,注释就不写了
    int[] hex2Int(String src, int dig) {
        byte[] b = src.getBytes();
        int len = (b.length%dig == 0) ? b.length/dig : b.length/dig+1;
        int[] result = new int[len];
        int[] tmp = new int[dig];
        Arrays.fill(tmp, 0);
        int remain = 0;
        for (int i=0, j=0, k=0; i<b.length; i++) {
            if (b[i]>=(byte)'0' && b[i]<=(byte)'9') {
                tmp[j++] = (int)(b[i]-(byte)'0');
            } else if (b[i]>=(byte)'a' && b[i]<=(byte)'f') {
                tmp[j++] = 10 + (int)(b[i]-(byte)'a');
            } else if (b[i]>=(byte)'A' && b[i]<=(byte)'F') {
                tmp[j++] = 10 + (int)(b[i]-(byte)'A');
            } else {tmp[j++] = 0;}
            if (j==dig) {
                int base = 1;
                for (int m=j-1; m>=0; m--) {
                    result[k] += tmp[m]*base;
                    base *= 16;
                }
                Arrays.fill(tmp, 0);
                j = 0;
                k++;
            }
            remain = j;
        }
        int base = 1; //剩余的字符串
        for (int m=remain-1; m>=0; m--) {
            result[result.length-1] += tmp[m]*base;
            base *= 16;
        }    return result;
    }
      

  6.   

    调用时
    int[] r = hex2Int("(“3e5fa8”, 2); //按2位分割,如果是按3位分割就传参数3,等等
      

  7.   

    非常感谢。
    最后一个}上面加上return result;返回一个int[]就可以用了
    谢谢。。谢谢
      

  8.   


    int[] hex2Int(String src, int dig) {
        byte[] b = src.getBytes();
        int len = (b.length%dig == 0) ? b.length/dig : b.length/dig+1;
        int[] result = new int[len];
        int[] tmp = new int[dig];
        Arrays.fill(tmp, 0);
        int remain = 0;
        for (int i=0, j=0, k=0; i<b.length; i++) {
            if (b[i]>=(byte)'0' && b[i]<=(byte)'9') {
                tmp[j++] = (int)(b[i]-(byte)'0');
            } else if (b[i]>=(byte)'a' && b[i]<=(byte)'f') {
                tmp[j++] = 10 + (int)(b[i]-(byte)'a');
            } else if (b[i]>=(byte)'A' && b[i]<=(byte)'F') {
                tmp[j++] = 10 + (int)(b[i]-(byte)'A');
            } else {tmp[j++] = 0;}
            if (j==dig) {
                int base = 1;
                for (int m=j-1; m>=0; m--) {
                    result[k] += tmp[m]*base;
                    base *= 16;
                }
                Arrays.fill(tmp, 0);
                j = 0;
                k++;
            }
            remain = j;
        }
        if (remain > 0) { //上面的,这里漏了判断有没有剩余了
            int base = 1; 
             for (int m=remain-1; m>=0; m--) {
                result[result.length-1] += tmp[m]*base;
                base *= 16;
            }
        }
        return result;
    }