Yes they do

Solved

Posted on 2007-10-01

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;

}

-------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;

}

15 Comments

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

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..?

Or are you saying what I try to achive here is not possible in Java?

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

=

73345194530449474985153048

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

Title | # Comments | Views | Activity |
---|---|---|---|

Java MSI Solution | 3 | 52 | |

network + | 7 | 58 | |

reasons why a "inside Menu" would not appear for a web server | 6 | 34 | |

Unable to start eclipse ? | 17 | 48 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**23** Experts available now in Live!