# 60进制时间计算器_再谈进制转换

1 × 8 + 1 × 4 + 0 × 2 + 0 × 1 = 12

2 + 6 = 11

3 + 4 = 10

2 * 2 = 4

1 × 11 + 1 × 4 + 0 × 2 + 0 × 1 = 11 + 4 = 15

，结果一定是
8，这也决定了，我们只能将其它进制先转为十进制。
public int twoToTen(String two) {
char[] array = two.toCharArray();
int n = array.length;
int sum = 0;
int power = 0;
for (int i = n - 1; i >= 0; i--) {
sum = sum + (array[i] - '0') * (int) Math.pow(2, power);
power++;
}
return sum;
}

public final class Seven {
public static int sum(int n1, int n2) {
int res = 0;
int carry = 0;
int shift = 1;
while (n1 != 0 || n2 != 0) {
int x = (n1 != 0) ? n1 % 10 : 0;
int y = (n2 != 0) ? n2 % 10 : 0;
int sum = carry + x + y;
carry = sum / 7; //保存进位
res = res + sum % 7 * shift;
n1 /= 10;
n2 /= 10;
shift *= 10;
}
if (carry != 0) {
res = res + 1 * shift;
}
return res;
}

//传进来的数是七进制, 实现减 1
public static int subOne(int n) {
int count = 0;
//考虑需要借位的情况，比如这种 43000
while (n % 10 == 0) {
n /= 10;
count++;
}
n -= 1; //借位
//低位补 6
while (count != 0) {
n = n * 10 + 6;
count--;
}
return n;

}

public static int mul(int n1, int n2) {
if (n2 == 0) {
return 0;
}
return Seven.sum(n1, mul(n1, Seven.subOne(n2)));
}

public static int pow(int a, int b) {
if (b == 0) {
return 1;
}
return Seven.mul(a, pow(a, Seven.subOne(b)));
}
}

public int twoToSeven(String two) {
char[] array = two.toCharArray();
int n = array.length;
int sum = 0;
int power = 0;
for (int i = n - 1; i >= 0; i--) {
int pow = Seven.pow(2, power);
int mul = Seven.mul(array[i] - '0', pow);
sum = Seven.sum(sum, mul);
power++;
}
return sum;
}

public int anyToSeven(String two, int radix) {
char[] array = two.toCharArray();
int n = array.length;
int sum = 0;
int power = 0;
for (int i = n - 1; i >= 0; i--) {
int pow = Seven.pow(radix, power);
int mul = Seven.mul(array[i] - '0', pow);
sum = Seven.sum(sum, mul);
power++;
}
return sum;
}

ps: 上边的代码，没有做严格的测试，思想应该是对的，哈哈，发现问题可以和我交流。

## 总结一下

08-22
07-02 2795
09-15 1344
08-20
06-11
04-11
05-08
©️2020 CSDN 皮肤主题: 1024 设计师:白松林