我正在尝试从十六进制转换为二进制。我已经有了将十进制转换为二进制的代码,只是不知道如何从那里开始转换为二进制。
public static String hexToDec(String valueIn) {
int endNum = 0;
for (int i = 0; i < valueIn.length(); i++) {
char curDigit = valueIn.charAt(i);
endNum *= 16;
if (curDigit >= 'A' && curDigit <= 'F')
endNum += 10 + (curDigit - 'A');
else
endNum += curDigit - '0';
}
return "" + endNum;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter the decimal number: ");
String valueIn = keyboard.nextLine ();
String outputOut = hexToDec(valueIn);
System.out.println("The output is: " + outputOut);
}
public static String decimalToBinary(int valueIn) {
int remainder, i = 0;
String binaryNum = "";
while (valueIn != 0) {
remainder = valueIn % 2;
valueIn /= 2;
binaryNum = remainder+binaryNum;
}
return binaryNum;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter the decimal number: ");
int valueIn = keyboard.nextInt ();
String outputOut = decimalToBinary(valueIn);
System.out.println("The output is: " + outputOut);
}
小唯快跑啊
侃侃无极
紫衣仙女
相关分类