从 Base-10 转换为 Base-9 的 Java 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3888613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:51:54  来源:igfitidea点击:

Java code to convert from Base-10 to Base-9

javanumber-systems

提问by Emil

How to convert a long number in base 10 to base 9 without converting to string ?

如何将基数为 10 的长数转换为基数 9 而不转换为字符串?

回答by MAK

FWIW, all values are actually in base 2 inside your machine (I bet you already knew that). It only shows up as base 10 because string conversion creates string representations in base 10 (e.g. when you print), because methods like parseLongassumes the input string is in base 10 and because the compiler expects all literals to be in base 10 when you actually write code. In other words, everything is in binary, the computer only converts stuff into and from base 10 for the convenience of us humans.

FWIW,所有值实际上都在您机器内的基数 2 中(我打赌您已经知道了)。它只显示为基数 10,因为字符串转换创建基数为 10 的字符串表示(例如,当您打印时),因为像这样parseLong的方法假定输入字符串是基数 10 并且因为编译器期望所有文字在您实际编写时都在基数 10 中代码。换句话说,一切都是二进制的,计算机只是为了我们人类的方便而将内容与基数 10 相互转换。

It follows that we should be easily able to change the output base to be something other than 10, and hence get string representations for the same value in base 9. In Java this is done by passing an optional extra base parameter into the Long.toStringmethod.

因此,我们应该能够轻松地将输出基数更改为 10 以外的其他值,从而获得基数 9 中相同值的字符串表示形式。在 Java 中,这是通过将可选的额外基数参数传递给Long.toString方法来完成的。

long x=10;
System.out.println(Long.toString(x,9));

回答by mR_fr0g

Long base10 = 10;
Long.valueOf(base10.toString(), 9);

回答by Grodriguez

What does "convert to base 9 without converting to string" actually mean?

“在不转换为字符串的情况下转换为基数 9”实际上是什么意思?

Base-9, base-10, base-2 (binary), base-16 (hexadecimal), are just ways to representnumbers. The value itself does not depend on how you represent it. int x = 256is exactly the same as int x = 0xffas far as the compiler is concerned.

Base-9、base-10、base-2(二进制)、base-16(十六进制)只是表示数字的方式。值本身不取决于您如何表示它。int x = 256int x = 0xff编译器而言,完全相同。

If you don't want to "convert to string" (I read this as meaning you are not concerned with the representation of the value), then what do you want to do exactly?

如果您不想“转换为字符串”(我认为这意味着您不关心值的表示),那么您到底想做什么?

回答by Michael Clerx

You can't convert to base 9 without converting to string.

如果不转换为字符串,则无法转换为基数 9。

When you write

当你写

Long a = 123;

you're making the implicit assumption that it's in base 10. If you want to interpret that as a base 9 number that's fine, but there's no way Java (or any other language I know of) is suddenly going to see it that way and so 8+1 will return 9 and not 10. There's native support for base 2, 8, 16 and 10 but for any other base you'll have to treat it as a string. (And then, if you're sure you want this, convert it back to a long)

你在隐含假设它在基数 10 中。如果你想将它解释为基数 9 很好,但是 Java(或我知道的任何其他语言)不可能突然以这种方式看待它并且所以 8+1 将返回 9 而不是 10。有对基数 2、8、16 和 10 的本机支持,但对于任何其他基数,您必须将其视为字符串。(然后,如果你确定你想要这个,把它转换回一个长的)

回答by Luca Martini

You have to apply the algorithm that converts number from one base to another by applying repeated modulo operations. Look herefor a Java implementation. I report here the code found on that site. The variable Mmust contain the number to be converted, and Nis the new base. Caveat: for the snippet to work properly, N>=1 && N<=10must be true. The extension with N>10is left to the interested reader (you have to use letters instead of digits).

您必须通过应用重复的模运算来应用将数字从一种基数转换为另一种基数的算法。看看这里的Java实现。我在这里报告在该站点上找到的代码。变量M必须包含要转换的数字,并且N是新的基数。警告:要使代码段正常工作,N>=1 && N<=10必须为真。扩展名N>10留给感兴趣的读者(您必须使用字母而不是数字)。

String Conversion(int M, int N) // return string, accept two integers
{   
    Stack stack = new Stack();  // create a stack
    while (M >= N)  // now the repetitive loop is clearly seen
    {   
        stack.push(M mod N);    // store a digit
        M = M/N;    // find new M
    }   
    // now it's time to collect the digits together 
    String str = new String(""+M);  // create a string with a single digit M
    while (stack.NotEmpty())    
        str = str+stack.pop()   // get from the stack next digit
    return str; 
}

回答by Czipperz

If you LITERALLY can do anything but convert to string do the following:

如果您实际上除了转换为字符串之外什么都可以做,请执行以下操作:

public static long toBase(long num, int base) {
    long result;
    StringBuilder buffer = new StringBuilder();
    buffer.append(Long.toString(num, base));
    return Long.parseLong(buffer.toString());
}