Panadol Chong
Panadol Chong

Reputation: 1903

Convert.ToInt64 equivalent in java

I try the following code in c# and it give me the result as follow:

long dec1 = Convert.ToInt64("B62FD56EFD5B375D", 16);

result : -531879796222753398

I am trying to do this in java, but I always get NumberFormatException, because there are alphanumeric inside the String. What I code in java is:

Long.parseLong("B62FD56EFD5B375D", 16);

May I know what is the equivalent of this in java?

Upvotes: 1

Views: 3026

Answers (4)

Mac D'zen
Mac D'zen

Reputation: 151

long dec1 = new BigInteger("B62FD56EFD5B375D", 16).longValue();

Upvotes: 1

Gayan Mettananda
Gayan Mettananda

Reputation: 1518

Maximum value is 9,223,372,036,854,775,807 (inclusive) for a long value. When the value B62FD56EFD5B375D parsed it is 13,127,946,111,482,018,682 which is unable to hold in a long value.

So instead use BigInteger.

Upvotes: 1

Ousmane D.
Ousmane D.

Reputation: 56423

You can use Long.parseUnsignedLong in Java to get the same result.

long result = Long.parseUnsignedLong("B62FD56EFD5B375D", 16);

Upvotes: 4

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30545

you can try with BigInteger

BigInteger value = new BigInteger(hex, 16);

Upvotes: 0

Related Questions