Java 长到十六进制字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30529640/
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
Long to HEX string
提问by Chikage
I'm having trouble converting a hexadecimal number in a String to a long and then back again. Here's the code I'm using:
我在将 String 中的十六进制数转换为 long 然后再返回时遇到问题。这是我正在使用的代码:
private void parseExperiment(){
String str1 = "AA0F245C";
long nr = Long.parseLong(str1, 16);
String str2 = Double.toHexString(nr);
}
I want str2 to be the same as str1 but when I print str1, nr and str2 I get this:
我希望 str2 与 str1 相同,但是当我打印 str1、nr 和 str2 时,我得到了这个:
str1: AA0F245C
nr: 2853119068
str2: 0x1.541e48b8p31
Anyone knows how to fix this?
有谁知道如何解决这个问题?
回答by Chikage
Use Long.toHexString
if you want to convert the long
back again.
使用Long.toHexString
,如果你要转换的long
再次回来。
Converting a long
to a hex string using Double.toHexString
will give you the proper representation of a double
which is different from that of a long
.
long
使用Double.toHexString
将a转换为十六进制字符串将为您提供与 adouble
不同的 a 的正确表示long
。
回答by Maciej
If you use Long.toHexString(nr);
you will get correct answer
如果你使用Long.toHexString(nr);
你会得到正确的答案