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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:46:19  来源:igfitidea点击:

Long to HEX string

javastringhexconverterlong-integer

提问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.toHexStringif you want to convert the longback again.

使用Long.toHexString,如果你要转换的long再次回来。

Converting a longto a hex string using Double.toHexStringwill give you the proper representation of a doublewhich 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);你会得到正确的答案