Java 负 BigInteger toString
我似乎对 Java 的 BigInteger 有一个二进制补码问题。 我有一个 64 位整数,其中只有 msb 和第二个 msb 设置为 1,其余为 0。
在十进制中,这达到:-4611686018427387904
我的应用程序的 Java 端接收这个十进制数作为字符串,并转换将其转换为 BigInteger,如下所示:
BigInteger bi = new BigInteger("-4611686018427387904", 10);
然后,它需要以二进制和十六进制形式显示该数字。 我尝试使用:
String bin = bi.toString(2);
String hex = bi.toString(16);
但我得到:
-100000000000000000000000000000000000000000000000000000000000000
-4000000000000000
而我期望得到:
1100000000000000000000000000000000000000000000000000000000000000
c000000000000000
有什么提示吗?
It seems I have a two's complement issue with Java's BigInteger.
I have a 64-bit integer where only the msb and the second msb are set to 1, the rest is 0.
In decimal this comes up to: -4611686018427387904
The Java side of my application receives this decimal number as a string, and converts it to BigInteger like so:
BigInteger bi = new BigInteger("-4611686018427387904", 10);
Then, it needs to display this number both in binary and hex forms.
I tried to use:
String bin = bi.toString(2);
String hex = bi.toString(16);
but I'm getting:
-100000000000000000000000000000000000000000000000000000000000000
-4000000000000000
whereas I expect to get:
1100000000000000000000000000000000000000000000000000000000000000
c000000000000000
Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
数字始终适合 64 位:
如果您的数字始终适合 64 位,您可以将其放入 long 中,然后打印位/十六进制数字。
数字可能并不总是适合 64 位:
如果数字不总是适合 64 位,则您必须“手动”解决它。要将数字转换为其二进制补码表示形式,请执行以下操作:
对于
BigInteger
,转换如下所示:如果使用
bi.toString(2)
打印它,则仍然会得到符号字符,而不是前导1
。只需将.replace('-', '1')
附加到字符串即可解决此问题。Number always fits in 64 bits:
If your number always fits in 64 bits you can put it in a long and then print the bits / hex digits.
Number may not always fit in 64 bits:
If the number does not always fit in 64 bits, you'll have to solve it "manually". To convert a number to it's two's complement representation you do the following:
For a
BigInteger
the conversion looks as follows:If you print it using
bi.toString(2)
you'll still get the sign character, instead of a leading1
. This can be solved by simply appending.replace('-', '1')
to the string.有一个
BigInteger.toByteArray()
方法,它将BigInteger
的二进制补码表示形式返回为byte[]
。您所需要做的就是以十六进制或二进制形式打印该数组:There is a
BigInteger.toByteArray()
method, that returns two's complement representation ofBigInteger
as abyte[]
. All you need is to print that array in hex or binary form:二进制数1100000000000000000000000000000000000000000000000000000000000000绝对是一个正数,对吧。它等于 2^63 + 2^62。
我不明白为什么当您转换为基数 2 或基数 16 时,您会期望负数变为正数。
您将基数 n 表示与数字的内部表示混淆了。
The binary number 1100000000000000000000000000000000000000000000000000000000000000 is definitely a positive number, right. It's equal to 2^63 + 2^62.
I don't see why you'd expect a negative number to become positive when you convert to base 2 or base 16.
You are confusing the base n representation with the internal representation of numbers.
如果数字为 64 位或更少,那么解决此问题的简单方法是转换为
long
,然后使用Long.toHexString()
。If the number is 64 bits or less, then the simple way to solve this is to convert to a
long
and then useLong.toHexString()
.你是什么意思?
你想得到二进制补码吗?
如果你是这个意思,也许我可以给你一个例子
what you mean?
Do you want to get Two's complement?
if you mean that, maybe i can give you an example