1

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[]

Denko Mancheski
  • 2,709
  • 2
  • 18
  • 26
  • Why screenshot when you can copy/paste? – Thomas Ayoub Nov 13 '15 at 12:22
  • 2
    Flip your byte array. Java uses Big-Endian, C# (most likely) uses little-endian. More infos: [Wikipedia](https://en.wikipedia.org/wiki/Endianness) – a-ctor Nov 13 '15 at 12:22
  • 1
    Possible duplicate of [How to convert a sbyte\[\] to byte\[\] in C#?](http://stackoverflow.com/questions/829983/how-to-convert-a-sbyte-to-byte-in-c) – Thomas Ayoub Nov 13 '15 at 12:23
  • I flipped my byte[] array to sbyte[] but now I need to create BigInteger from sbyte[] and the BigInteger constructor does not accept sbyte[] – Denko Mancheski Nov 13 '15 at 12:24
  • I am not trying to convert byte[] to sbyte[] , its already done, I am trying to create BigInteger from sbyte[] instead of byte[] and I am not able to – Denko Mancheski Nov 13 '15 at 12:25
  • Changing a `byte[]` to an `sbyte[]` is not flipping an array. e.g. normal: `[23, 14, 25, 64]` flipped: `[64, 25, 14, 23]` – a-ctor Nov 13 '15 at 12:26
  • The array order is fine. arrName[2] in java is -41 and in c# is 215 – Denko Mancheski Nov 13 '15 at 12:28
  • Why do you need to create a `BigInteger` from an `sbyte[]`? Just take the `byte[]` – a-ctor Nov 13 '15 at 12:29
  • Because after I get the BigInteger value, i need to encode it to base36 and substring to get some "id" that I need to send on a rest server. And since I am getting the BigInteger from unsigned bytes, I get different "id" than what I am supposed to – Denko Mancheski Nov 13 '15 at 12:32
  • @DenkoMancheski I don't understand what you are trying to do. Changing a `sbyte[]` into an `byte[]` won't change the binary representation. Therefor it should work fine. – a-ctor Nov 13 '15 at 12:36
  • I added screenshot of what exactly the problem is – Denko Mancheski Nov 13 '15 at 12:43

1 Answers1

3

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.

a-ctor
  • 3,568
  • 27
  • 41
  • How could I ... Thanks a lot, I thought this little-endian thing is connected with the conversion or something – Denko Mancheski Nov 13 '15 at 13:00
  • Signed or unsigned have nothing to do with little or big endian. The sbyte[] conversion only re-interprets unsigned as signed. It does not reverse the entire array. – Rudy Velthuis Nov 16 '15 at 08:37