Denko Mancheski
Denko Mancheski

Reputation: 2719

C# sbyte array to BigInteger

enter image description hereI have to convert sbyte array to BigInteger. I am on universal windows platform. The BigInteger constructor only accepts byte[] and not sbyte[]. I cannot use byte[] since in C# byte is unsigned by default and I get higher values than 128.

The sbyte array is converted from byte array because when I compare the BigInteger value that C# is returning is different from the value in Java since java by default is using signed bytes and c# is using unsigned.

Any Solution ? BigInteger in java: 78214101938123633359912717791532276502 BigInteger in C# 30381787179362266836169791328776673082

Edit: I am trying to make a BigInteger from sbyte[] array. I am not trying to convert byte[] to sbyte[]

Upvotes: 1

Views: 914

Answers (1)

a-ctor
a-ctor

Reputation: 3733

Flip your byte array. Don't cast into an sbyte[]. Use a byte[].

Here are your values as arrays (singed and unsigned):

Java values (signed): 58, -41, 124, -12, 24, 102, 14, 93, 79, -102, 72, 47, -62, 81, -37, 22 Java values (unsigned): 58, 215, 124, 244, 24, 102, 14, 93, 79, 154, 72, 47, 194, 81, 219, 22

C# values (signed): 22, -37, 81, -62, 47, 72, -102, 79, 93, 14, 102, 24, -12, 124, -41, 58
C# values (unsigned): 22, 219, 81, 194, 47, 72, 154, 79, 93, 14, 102, 24, 244, 124, 215, 58

Take a look at Endianess. As stated before Java uses big-endian, C# uses little-endian. Therefor flip your byte[] and stop casting it into an sbyte[]. It is not what you want.

Upvotes: 3

Related Questions