入力を 8 進数から 2 進数または 16 進数に変換するプログラムを作成する予定ですが、事前に作成された API ルーチンを使用して変換を行うことはできません。私の最善の策は、それらをリバースエンジニアリングして、それらがどのように機能するかを確認することだと思います. その情報やその他の提案を提供できる情報源を知っている人はいますか? ありがとうございました!
2 に答える
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)));
楽しみのために、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();
}
上記はあまり効率的ではありません。:)
int 用の JDK のパーサーは次のとおりです。
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 に答える