Конвертер систем счисления
- private static String intToRadix(int value, int radix) {
- final StringBuilder sb = new StringBuilder();
- while (value > 0) {
- int v = value % radix;
- if (v >= 10) v = v - 10 + 'A';
- else v = v + '0';
- sb.append((char)v);
- value = value / radix;
- }
- return sb.reverse().toString();
- }
- private static int radixToInt(String value, int initialRadix) {
- int out = 0;
- final int len = value.length();
- int pow = 1;
- for (int i = 0; i < len; i++) {
- char ch = value.charAt(len - i - 1);
- int code;
- if (ch >= 'A') code = ch + 10 - 'A';
- else code = ch - '0';
- out += code * pow;
- pow *= initialRadix;
- }
- return out;
- }
Пример вывода:
3054072 in 36 -> 1TGJC in 10 -> 3054072
9896109 in 64 -> \g2d in 10 -> 9896109
16777215 in 512 -> vȶȶ in 10 -> 16777215
16777215 in 1024 -> Fжж in 10 -> 16777215
5465987 in 32768 -> Ý枺 in 10 -> 5465987
1396211 in 64000 -> L찪 in 10 -> 1396211
3054072 in 36 -> 1TGJC in 10 -> 3054072
9896109 in 64 -> \g2d in 10 -> 9896109
16777215 in 512 -> vȶȶ in 10 -> 16777215
16777215 in 1024 -> Fжж in 10 -> 16777215
5465987 in 32768 -> Ý枺 in 10 -> 5465987
1396211 in 64000 -> L찪 in 10 -> 1396211