Chikage
Chikage

Reputation: 329

Long to HEX string

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:

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:

str1: AA0F245C
nr: 2853119068
str2: 0x1.541e48b8p31

Anyone knows how to fix this?

Upvotes: 23

Views: 51945

Answers (2)

Maciej
Maciej

Reputation: 9605

If you use Long.toHexString(nr); you will get correct answer

Upvotes: 12

user1907906
user1907906

Reputation:

Use Long.toHexString if you want to convert the long back again.

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.

Upvotes: 48

Related Questions