Java - 解析并将无符号十六进制字符串解析为带符号的long [英] Java - parse and unsigned hex string into a signed long
问题描述
d1bc4f7154ac9edb
我有一堆十六进制字符串,其中之一就是: code>
这是-3333702275990511909的十六进制值。如果你做了Long.toHexString(d1bc4f7154ac9edb);
现在,我们假设我只能访问十六进制字符串值,这就对了。这样做:
Long.parseLong(hexstring,16);
不起作用,因为它会将其转换为对于Long而言太大的不同值。是否有将这些无符号十六进制值转换为带符号的长整数?
谢谢!
您可以使用 BigInteger
来解析它并获取一个 long
:
long value = new BigInteger(d1bc4f7154ac9edb,16).longValue();
System.out.println(value); // this outputs -3333702275990511909
I have a bunch of hex strings, one of them, for example is:
d1bc4f7154ac9edb
which is the hex value of "-3333702275990511909". This is the same hex you get if you do Long.toHexString("d1bc4f7154ac9edb");
For now, let's just assume I only have access to the hex string values and that is it. Doing this:
Long.parseLong(hexstring, 16);
Doesn't work because it converts it to a different value that is too large for a Long. Is there away to convert these unsigned hex values into signed longs?
Thanks!
You can use BigInteger
to parse it and get back a long
:
long value = new BigInteger("d1bc4f7154ac9edb", 16).longValue();
System.out.println(value); // this outputs -3333702275990511909
这篇关于Java - 解析并将无符号十六进制字符串解析为带符号的long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!