-1

我要编写一个程序将输入从八进制转换为二进制或十六进制,但是我不能使用预先编写的 API 例程为我进行转换。我认为我最好的选择是对它们进行逆向工程,看看它们是如何运作的。有谁知道可以提供该信息或其他建议的来源?谢谢!

4

2 回答 2

0

您需要从基础开始,即将十进制数转换为二进制数。这个过程很简单。然后,你可以继续你所说的,因为过程是相似的。

正如其他人已经指出的那样,源代码与 jdk 捆绑在一起。

于 2013-10-13T22:10:16.613 回答
0

如果你下载了 intelliJ,那么你可以控制点击进入任何类。

另外,只需尝试使用谷歌搜索“java lang Integer 的源代码”

我做到了,它提出了:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Integer.java

这基本上就是您使用 JDK 的方式(正如有人指出的那样):

    String octalNo="037";
    System.out.println(Integer.toHexString(Integer.parseInt(octalNo, 8)));

为了好玩,我做了八进制问题:

    int octal = 037;

    System.out.println(octalToHexString(octal));

    int octal = 037;

    System.out.println(octalToHexString(octal));

}

public static String octalToHexString(int octal) {

    final  char[] hex = {
            '0' , '1' , '2' , '3' , '4' , '5' ,
            '6' , '7' , '8' , '9' , 'A' , 'B' ,
            'C' , 'D' , 'E' , 'F'
    };


    int val = octal;
    int radix = 0;
    int mask = 0;

    StringBuilder builder = new StringBuilder("0x");

    if (val==0) {
        return "0x" + 0;
    }

    while (val != 0) {

        radix = 1 << 4;
        mask = radix - 1;
        builder.insert(2, hex[val & mask]);
        val >>>= 4;
    }

    return builder.toString();

}

上面的效率不是很高。:)

这是来自 JDK 的 int 解析器:

public static int parseInt(String s, int radix)
            throws NumberFormatException
{
    /*
     * WARNING: This method may be invoked early during VM initialization
     * before IntegerCache is initialized. Care must be taken to not use
     * the valueOf method.
     */

    if (s == null) {
        throw new NumberFormatException("null");
    }

    if (radix < Character.MIN_RADIX) {
        throw new NumberFormatException("radix " + radix +
                                        " less than Character.MIN_RADIX");
    }

    if (radix > Character.MAX_RADIX) {
        throw new NumberFormatException("radix " + radix +
                                        " greater than Character.MAX_RADIX");
    }

    int result = 0;
    boolean negative = false;
    int i = 0, len = s.length();
    int limit = -Integer.MAX_VALUE;
    int multmin;
    int digit;

    if (len > 0) {
        char firstChar = s.charAt(0);
        if (firstChar < '0') { // Possible leading "+" or "-"
            if (firstChar == '-') {
                negative = true;
                limit = Integer.MIN_VALUE;
            } else if (firstChar != '+')
                throw NumberFormatException.forInputString(s);

            if (len == 1) // Cannot have lone "+" or "-"
                throw NumberFormatException.forInputString(s);
            i++;
        }
        multmin = limit / radix;
        while (i < len) {
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit(s.charAt(i++),radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            }
            if (result < multmin) {
                throw NumberFormatException.forInputString(s);
            }
            result *= radix;
            if (result < limit + digit) {
                throw NumberFormatException.forInputString(s);
            }
            result -= digit;
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
    return negative ? result : -result;
}
于 2013-10-13T23:17:14.507 回答