如何将用作 BigInteger 类型的 int 数组转换为“人类可读的”数组细绳?
我正在尝试实现 BigInteger 的一些功能作为个人编程练习。
与许多实现一样,我使用 int[]
作为无符号整数。
我想实现加法、减法、乘法、除法等基本功能,但遇到的问题是我需要从数据结构中获取“人类可读”的 toString
以便进行调试,所以我可以更好地检查和理解我在做什么。
我感觉我被困住了。我不确定我的算法是否正确,但我没有办法检查它。
我研究过一些实现,例如 Apache Harmony 或 OpenJDK,但它们用于创建字符串的算法看起来比加号、减号等的实际实现更复杂。
当然,我可以只使用那些复杂的之一,但如果我自己已经无法实现它,我至少希望能够理解它的实现。
有人可以建议一个将 int[]
转换为 String 的简单实现吗?
示例:new int[]{Integer.MAX_VALUE, 1}
应被视为一个大的无符号数字并打印:8589934590
(所以基本上是 2³³)。
I'm trying to implement some functionality of BigInteger
s as a personal programming exercise.
Like many implementations I use an int[]
as an unsigned integer.
I want to implement basic functionality like addition, subtraction, multiplication, division, but I'm hitting the problem that I need to get a "human readable" toString
out of my data structure for debug purposes, so that I can better inspect and understand what I'm doing.
I feel like I'm stuck. I'm not confident that my algorithms are right, but I have no way to check it.
I have looked at some implementations like Apache Harmony or OpenJDK, but the algorithms they use to create the String look more complex than the actual implementation of plus, minus, ... and so on.
Of course I could just use one of those complicated ones, but I would at least want to be able to understand the implementation of it, if I already fail at implementing it myself.
Can someone suggest a simple implementation which converts an int[]
to a String?
Example: new int[]{Integer.MAX_VALUE, 1}
should be treated as one large, unsigned number and print: 8589934590
(So basically 2³³).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用这种方法:
int[]
;称之为iArr
。该算法的其余部分将在iArr
上运行。char[]
(将其称为s
)。由于int
最多可以有十位数字,因此您可以使用s = new char[iArr.length * 10]
。int i = s.length - 1
开始。遍历iArr
,除以 10,并将余数加上'0'
存储在s[i]
中。然后递减i
。重复此过程,直到 iArr 为零。 (除以 10 的逻辑大致是:将每个元素除以 10,记下余数。将余数乘以Integer.MAX_INT + 1
添加到下一个元素,然后除那个< /em> 元素乘以十。不用说,您需要使用long
进行数学计算。)new String(s, i, s.length - i)< /代码>。
为了提高效率:
1000000
或诸如此类的东西,一次获得一堆数字(显然这使得char
处理不过比较棘手)。但实际上这两者都没有必要。
编辑以添加实际代码。该程序:
打印以下内容:(
但是您必须检查
main
方法及其注释,以确认我已正确理解您打算如何存储这些整数。)I'd use this approach:
int[]
; call itiArr
. The rest of this algorithm will operate oniArr
.char[]
— call its
— that is big enough. Since anint
can have up to ten digits, you can uses = new char[iArr.length * 10]
.int i = s.length - 1
. Go throughiArr
, dividing by ten, and store the remainder plus'0'
ins[i]
. Then decrementi
. Repeat this process untiliArr
is zero. (The logic for dividing by ten is roughly: divide each element by ten, noting the remainder. Add that remainder, timesInteger.MAX_INT + 1
, to the next element, before dividing that element by ten. Needless to say, you'll need to do your math usinglong
.)new String(s, i, s.length - i)
.For efficiency's sake:
1000000
or whatnot, to get a bunch of digits at once (obviously this makes thechar
-handling trickier, though).iArr
, you can keep track of where the first non-zero element is, and ignore any elements before that.but neither of those is actually necessary.
Edited to add actual code. This program:
prints this:
(But you'll have to examine the
main
method, with its comments, to confirm that I've correctly understood how you intend to store these integers.)首先实现十六进制转换器,并在开发的初始阶段使用十六进制作为人类可读的格式。执行十六进制很容易,因为您不需要实现除法:只需将各个项目转换为十六进制,将它们粘合在一起,就完成了。
一旦您的除法算法到位,您就可以通过通常的“获取余数 % 10 并除掉”方法实现到十进制格式的转换。
Start by implementing a converter to hex, and use hex as your human-readable format during the initial stages of development. Doing HEX is easy, because you do not need to implement division: simply convert individual items to HEX, glue them together, and you are done.
Once your division algorithm is in place, you can implement conversion to decimal format by the usual "get the remainder % 10 and divide away" approach.