No hexadecimal parsing, formatting for BigInteger?

X

xarax

I was using Long.toHexString() and Long.parseLong() for dealing
with 64-bit integers. I now need to handle 128-bit integers, so
I looked at java.math.BigInteger, but it doesn't have hexadecimal
(or any base conversion/formatting) methods.

How should I parse "0xa9ce09230dea0b9032340ade9034d334" into
a BigInteger? How should I get a BigInteger as a hexadecimal
string?
 
C

Chris Uppal

xarax said:
How should I parse "0xa9ce09230dea0b9032340ade9034d334" into
a BigInteger?

I suspect that:
BigInteger big = new BigInteger("a9ce09230dea0b9032340ade9034d334", 16);
would do the trick, though I haven't tried it.
How should I get a BigInteger as a hexadecimal
string?

and:
String str = big.toString(16);
should reverse it, though I haven't tried that either...

-- chris
 
R

Roedy Green

I was using Long.toHexString() and Long.parseLong() for dealing
with 64-bit integers. I now need to handle 128-bit integers, so
I looked at java.math.BigInteger, but it doesn't have hexadecimal
(or any base conversion/formatting) methods.

see http:mindprod.com/hex.html
for how to convert an arbitrarily long byte array to hex and back.

The problem with longs is they are signed, and rarely do you want
signed hex.
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

xarax said:
I was using Long.toHexString() and Long.parseLong() for dealing
with 64-bit integers. I now need to handle 128-bit integers, so
I looked at java.math.BigInteger, but it doesn't have hexadecimal
(or any base conversion/formatting) methods.

How should I parse "0xa9ce09230dea0b9032340ade9034d334" into
a BigInteger?

You need a method to convert the hex string to an integer string, or a
byte array. Then pass that to the BigInteger constructor.

Here is a small method to convert a hex String to a byte array. This
method doesn't use the 0x prefix, and it assumes that the String is of
even length (you can't convert f, instead you must use 0f). Also,
remember that you need to use the BigInteger(int signum, byte[]
magnitude) constructor with signum = 1.

static byte[] stringToBytes(CharSequence str)
{
int len = str.length();
if (len % 2 != 0)
{
throw new IllegalArgumentException("Sequence length not even: " + len);
}

byte[] b = new byte[len / 2];

for (int i = len - 1; i > 0; i -= 2)
{
char c = str.charAt(i);

int bt = 0;

// 0-9
if (c >= 0x30 && c <= 0x39)
{
bt += c - 0x30;
}
// A-F (uppercase)
else if (c >= 0x41 && c <= 0x46)
{
bt += c - 0x37;
}
// a-f (lowercase)
else if (c >= 0x61 && c <= 0x66)
{
bt += c - 0x57;
}
else
{
throw new IllegalArgumentException("Not a hex value: " + c + " at
position: " + i);
}

char cNext = str.charAt(i - 1);

if (cNext >= 0x30 && cNext <= 0x39)
{
bt += (cNext - 0x30) * 16;
}
else if (cNext >= 0x41 && cNext <= 0x46)
{
bt += (cNext - 0x37) * 16;
}
else if (cNext >= 0x61 && cNext <= 0x66)
{
bt += (cNext - 0x57) * 16;
}
else
{
throw new IllegalArgumentException("Not a hex value: " + c + " at
position: " + (i - 1));
}

b[i / 2] = (byte) bt;
}
return b;
}
How should I get a BigInteger as a hexadecimal
string?

The reverse of above.
 
X

xarax

I was using Long.toHexString() and Long.parseLong() for dealing
with 64-bit integers. I now need to handle 128-bit integers, so
I looked at java.math.BigInteger, but it doesn't have hexadecimal
(or any base conversion/formatting) methods.

How should I parse "0xa9ce09230dea0b9032340ade9034d334" into
a BigInteger? How should I get a BigInteger as a hexadecimal
string?

Never mind. I decided to write my own static methods that
use the BigInteger constructor and the toString(int) instance
method. Seems like over-kill, but there's nothing else.

:blah:
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Daniel said:
You need a method to convert the hex string to an integer string, or a
byte array. Then pass that to the BigInteger constructor.
<snip>
Although, of course you could use the radix constructor and toString, as
somebody else said :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top