|
|
|
@ -5,8 +5,46 @@ import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
|
|
|
|
|
public class computer {
|
|
|
|
|
public static int convertToDecimal(String number, int base) {
|
|
|
|
|
int decimalValue = 0;
|
|
|
|
|
int power = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = number.length() - 1; i >= 0; i--) {
|
|
|
|
|
char digit = number.charAt(i);
|
|
|
|
|
int digitValue;
|
|
|
|
|
|
|
|
|
|
if (Character.isDigit(digit)) {
|
|
|
|
|
digitValue = digit - '0';
|
|
|
|
|
} else {
|
|
|
|
|
digitValue = Character.toUpperCase(digit) - 'A' + 10; // A-F
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decimalValue += digitValue * Math.pow(base, power);
|
|
|
|
|
power++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return decimalValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String convertFromDecimal(int number, int base) {
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
if (number == 0) {
|
|
|
|
|
return "0";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (number > 0) {
|
|
|
|
|
int remainder = number % base;
|
|
|
|
|
if (remainder < 10) {
|
|
|
|
|
result.append((char) ('0' + remainder));
|
|
|
|
|
} else {
|
|
|
|
|
result.append((char) ('A' + remainder - 10));
|
|
|
|
|
}
|
|
|
|
|
number /= base;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.reverse().toString();
|
|
|
|
|
}
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
JFrame frame = new JFrame("进制转换器");
|
|
|
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|