We help IT Professionals succeed at work.

# Need help verify codes using unsigned BigInteger

on
1,027 Views
I need help converting the C# code below to Java. Attached are both C# and Java code, I need someone to verify if both codes below produce the same value. I am not C# person and I don't know how to test the C# code below.

-------Java codes -----------
public static BigInteger CombineIntoNumber(byte[] data)
{
int index;
int count;
BigInteger number = new BigInteger("0");
count = data.length;

// compute a numeric representation of the binary data
for (index = 0; index < count; ++index)
{
// shift the number one byte over
number = number.shiftLeft(8);
number = new BigInteger(1, number.toByteArray());    //make it unsigned value

// add the number for the current byte
number = number.add(new BigInteger(new byte[] {data[index]}));
}
return number;
}
-------C# codes--------------
public static ulong CombineIntoNumber(byte[] data)
{
int index;
int count;
ulong number;
number = 0;
count = data.Length;

// compute a numeric representation of the binary data
for (index = 0; index < count; ++index)
{
// don't throw an OverflowException, just let it roll over
unchecked
{
// shift the number one byte over
number <<= 8;

// add the number for the current byte
number += data[index];
}
}
return number;
}
Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Top Expert 2016

Commented:
Yes they do
CERTIFIED EXPERT
Top Expert 2016

Commented:
Of course, your Java body could be written

return new BigInteger(1, data);

Commented:
I think both Java and C# code return the same value if the number is small. If the number is beyond the max long value it will return different value.
Can someone try it with input string value '1/PEPCCC009991111ABCD'?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>If the number is beyond the max long value it will return different value.

Yes, but surely that goes without saying? ulong is a limited precision value, unlike BigInteger

>>Can someone try it with input string value '1/PEPCCC009991111ABCD'?

But that's not a number..?
CERTIFIED EXPERT
Top Expert 2016

Commented:
You should use System.Numeric.BigInteger where possible

Commented:
I thought BigInteger is equivalent to C# ulong. What I try to get here is to have Java code that can handle a large long value. The code above is processing byte[] type, convert the string input '2/PEPCCC009991111ABCD' to byte type to test the code above.

Or are you saying what I try to achive here is not possible in Java?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>I thought BigInteger is equivalent to C# ulong.

No - BigInteger is arbitrary precision, ulong is limited precision. The equiavalent in .NET is System.Numeric.BigInteger

>>convert the string input '2/PEPCCC009991111ABCD' to byte type to test the code above.

Oh, you mean use the byte[] as if it were a number? I'll tell you what it is
CERTIFIED EXPERT
Top Expert 2016

Commented:
"2/PEPCCC009991111ABCD"
=
73345194530449474985153048851406816633303297311556

which, needless to day, won't fit in a ulong ;-)

Commented:
Do you have any suggestion what I should use in Java to handle a number that big?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Do you have any suggestion what I should use in Java to handle a number that big?

BigInteger

Commented:
That is what I am using in Java and it still didn't produce the same outcome as the C# code.
CERTIFIED EXPERT
Top Expert 2016

Commented:
I've already explained - the C# code cannot possibly handle that number, so its output is of no importance

Commented:
I don't think so. The C# code is working because it is using uncheck which will wrap around the value. I am trying to convert a working code to Java.
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
CERTIFIED EXPERT
Top Expert 2016

Commented:
Please post the C# result from "2/PEPCCC009991111ABCD"
Unlock the solution to this question.