[英]Converting 64-bit unsigned int (in hex string form) to decimal in java
I have a hex string that represents a 64-bit unsigned int. 我有一个表示64位无符号整数的十六进制字符串。
I want to convert this to a decimal representation in java. 我想将其转换为Java中的十进制表示形式。
I wanted to use Long.parseLong("HexString", 16);
我想使用Long.parseLong("HexString", 16);
but in java the leftmost bit is the sign bit and any number greater than 2^31-1 would not fit. 但是在Java中,最左边的位是符号位,任何大于2 ^ 31-1的数字都不适合。
SO how do i convert and store this data? 那么我该如何转换和存储这些数据?
Thanks, Sunny 谢谢,阳光明媚
You can create a BigInteger
using the constructor that takes a String
and an int
radix . 您可以使用采用String
和int
radix的构造函数来创建BigInteger
。
BigInteger bigInt = new BigInteger(hexString, 16);
If you have Java 8, you can use Long.parseUnsignedLong
, which will allow you to parse all 64 bits, even if the bit that would normally put it over Long.MAX_VALUE
is set. 如果您具有Java 8,则可以使用Long.parseUnsignedLong
,即使设置了通常将其放置在Long.MAX_VALUE
的位,它也可以解析所有64位。 It still returns a long
, which in Java is still signed, but it will be parsed as if it were an unsigned long
. 它仍然返回long
,在Java中仍然是带符号的,但是它将被解析为好像是一个无符号的long
。
long stillSigned = Long.parseUnsignedLong(hexString, 16);
If the value that gets parsed is negative, then the true unsigned value is the signed value + 2 64 . 如果要解析的值为负,则真正的无符号值是有符号值+ 2 64 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.