3

我知道将任何基础转换为任何基础都很简单且可能。首先,将任何基数转换为十进制,然后将十进制转换为任何其他基数。但是,我之前在 2 到 36 范围内这样做过,但从未在 2 到 46 范围内这样做过。

我不明白我会在 36 之后放什么,因为 36 表示“z”(1-10 是十进制数字,然后是字母表的 26 个字符)。

请解释 36 岁之后会发生什么。

4

5 回答 5

5

您用于数字的符号是任意的。例如,base64 编码使用“A”表示零值数字,“0”表示值为 52 的数字。在 base64 中,数字经过字母 AZ,然后是小写字母 az,然后是传统数字 0-9 , 然后通常是 '+' 和 '/'。

一个 base 60 系统使用了这些符号:巴巴罗尼亚数字

所以使用的符号是任意的。36 岁之后没有什么“发生”,除了你所说的发生在你的系统上。

于 2012-09-25T07:04:06.010 回答
5

每个基地都有一个目的。通常我们会进行基础转换以使复杂的计算更简单。

以下是一些最常用的碱基及其代表。

  • 2-二进制数字系统

    几乎所有计算机在内部使用,是基数二。两位数为01,分别由显示 OFF 和 ON 的开关表示。

  • 8八进制

    偶尔用于计算。八位数字是0–7

  • 10 进制

    世界上最常用的数字系统,用于算术。它的十位数是0–9

  • 12-十二进制(十进制)系统

    由于可被 2、3、4 和 6 整除,因此经常使用它。它传统上用作以几十和毛表示的数量的一部分。

  • 16 进制

    常用于计算。十六位数字0–9后跟A–F

  • 60进制

    起源于古苏美尔,传给巴比伦人。它仍然被用作我们现代圆坐标系(度、分和秒)和时间测量(分和小时)的基础。

  • 64-基地 64

    也偶尔用于计算,用作数字A–Z, a–z, 0–9,再加上两个字符,通常是+/

  • 256 字节

    由计算机内部使用,实际上将八个二进制数字组合在一起。对于人类阅读,字节通常以十六进制显示。

八进制、十六进制和 base-64 系统经常用于计算,因为它们易于作为二进制的简写。例如,每个十六进制数字都有一个等效的 4 位二进制数。

基数通常是自然数。然而,其他位置系统也是可能的,例如黄金比例底(其基数是非整数代数数)和负底(其基数为负)。


您的疑问是我们是否可以在基数超过 36 后将任何基数转换为任何其他基数(# of Alphabets + # of digits = 26+ 10= 36)

以 64-Base 为例,它使用 A-Z(大写)(26)、a-z(小写)(26)、0-9(10),再加上 2 个字符。这样就解决了 36 的约束。

由于我们在 64 进制中有 (26+26+10+2)64 个符号用于表示,因此我们可以在 64 进制中表示任何数字。同样,对于更多的基础,他们使用不同的符号来表示。

来源http ://en.wikipedia.org/wiki/Radix

于 2012-09-25T07:11:59.190 回答
3
于 2012-09-25T08:07:46.430 回答
1

要记住的重要一点是,所有数字都是价值的象征。因此,如果您想这样做,您可以制作一个包含每个位置的值的列表。在 base 36 之后,您只需用完可以构成逻辑序列的字符。例如,如果您使用包含 70 个奇数字符的柬埔寨字母表,则可以使用 80 进制。

于 2012-09-25T07:03:54.700 回答
0

Here is the complete code I have written, hope this will help.

import java.util.Scanner;

    /* 
    * author : roottraveller, nov 4th 2017
    */

public class BaseXtoBaseYConversion {

    BaseXtoBaseYConversion() {
    }

    public static String convertBaseXtoBaseY(String inputNumber, final int inputBase, final int outputBase) {
        int decimal = baseXToDecimal(inputNumber, inputBase);
        return decimalToBaseY(decimal, outputBase);
    }

    private static int baseXNumeric(char input) {
        if (input >= '0' && input <= '9') {
            return Integer.parseInt(input + "");
        } else if (input >= 'a' && input <= 'z') {
            return (input - 'a') + 10;
        } else if (input >= 'A' && input <= 'Z') {
            return (input - 'A') + 10;
        } else {
            return Integer.MIN_VALUE;
        }
    }

    public static int baseXToDecimal(String input, final int base) {
        if(input.length() <= 0) {
            return Integer.MIN_VALUE;
        }

        int decimalValue = 0;
        int placeValue = 0;

        for (int index = input.length() - 1; index >= 0; index--) {
            decimalValue += baseXNumeric(input.charAt(index)) * (Math.pow(base, placeValue));
            placeValue++;
        }

        return decimalValue;
    }

   private static char baseYCharacter(int input) {
        if (input >= 0 && input <= 9) {
            String str = String.valueOf(input);
            return str.charAt(0);
        } else {
            return  (char) ('a' + (input - 10));
            //return  ('A' + (input - 10));
        }
    }

    public static String decimalToBaseY(int input, int base) {
        String result = "";

        while (input > 0) {
            int remainder = input % base;
            input = input / base;
            result = baseYCharacter(remainder) + result;  // Important, Notice the reverse order here
        }

        return result;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter : number baseX baseY");

        while(true) {
            String inputNumber = scanner.next();
            int inputBase      = scanner.nextInt();
            int outputBase     = scanner.nextInt();

            String outputNumber = convertBaseXtoBaseY(inputNumber, inputBase, outputBase);
            System.out.println("Result = " + outputNumber);
        }
    }
}
于 2017-11-04T06:53:55.273 回答