• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 937
  • Last Modified:

Need help verify codes using unsigned BigInteger

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;
        }
0
azi98
Asked:
azi98
  • 10
  • 5
1 Solution
 
CEHJCommented:
Yes they do
0
 
CEHJCommented:
Of course, your Java body could be written

return new BigInteger(1, data);

0
 
azi98Author 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'?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CEHJCommented:
>>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..?
0
 
CEHJCommented:
You should use System.Numeric.BigInteger where possible
0
 
azi98Author 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?
0
 
CEHJCommented:
>>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
0
 
CEHJCommented:
"2/PEPCCC009991111ABCD"
=
73345194530449474985153048851406816633303297311556

which, needless to day, won't fit in a ulong ;-)
0
 
azi98Author Commented:
Do you have any suggestion what I should use in Java to handle a number that big?
0
 
CEHJCommented:
>>Do you have any suggestion what I should use in Java to handle a number that big?

BigInteger
0
 
azi98Author Commented:
That is what I am using in Java and it still didn't produce the same outcome as the C# code.
0
 
CEHJCommented:
I've already explained - the C# code cannot possibly handle that number, so its output is of no importance
0
 
azi98Author 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.
0
 
CEHJCommented:
>>... which will wrap around the value

But that's the whole problem. The Java doesn't wrap it. It is able to print the entire numeric value of the byte array, which you say is your requirement...
0
 
CEHJCommented:
Please post the C# result from "2/PEPCCC009991111ABCD"
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now