0

If have the following BigInteger:

1379080579050447841330186236235223160927998000398161138225875482305250883605652677639242794753995315199229112894647269426499088162253680340518114657361569012095908691691924534414360438924914998

If I convert to byte[] using ToByteArray in C# I get a result but if I convert the same BigInteger in Java I get a different result. How can I reach the same result? I need the byte[] since I need to remove the padding and then transform the byte array resulting into a string

5
  • 6
    C# The individual bytes in the array returned by this method appear in little-endian order. vs Java The byte array will be in big-endian byte-order: Commented Apr 18, 2016 at 16:16
  • 4
    Java uses a big-endian representation, while .NET uses little-endian, that's as simple as this. See here. Commented Apr 18, 2016 at 16:16
  • I reopened the question, since the alleged duplicate question relates to parsing of hexadecimal encoded strings and was not related to the endian difference of the toByteArray/ToByteArray methods. Commented Apr 18, 2016 at 16:31
  • I don't think there is a sane reason to use a Byte[] ever. I suggest using a byte[]. Commented Apr 18, 2016 at 17:03
  • Yeah, sorry it's my mistake. We are using byte Commented Apr 18, 2016 at 17:05

1 Answer 1

1

As pointed out in the comments, the reason you're seeing different values between the byte[] from the C# BigInteger and the Java BigInteger is that the byte order is reversed: C# is little-endian and Java is big-endian. (See Java vs. C#: BigInteger hex string yields different result? for more details).

As you can see, the order of the bytes in the array is exactly reversed:

C#

BigInteger bi = BigInteger.Parse("1379080579050447841330186236235223160927998000398161138225875482305250883605652677639242794753995315199229112894647269426499088162253680340518114657361569012095908691691924534414360438924914998");
byte[] ba = bi.ToByteArray();
for (int i = 0; i < ba.Length; i++) {
    Console.Write(ba[i] + " ");
}
// 54 49 48 50 47 52 48 47 49 50 32 111 110 114 111 105 103 32 44 98 117 108 67 
// 32 97 116 105 118 69 32 44 101 108 97 117 113 115 97 80 32 101 110 111 105 108 
// 103 101 86 32 111 116 110 101 118 69 32 44 52 57 57 49 47 56 48 47 49 50 32 44 
// 105 115 115 111 82 32 111 99 114 97 77 

Java

BigInteger bi = new BigInteger("1379080579050447841330186236235223160927998000398161138225875482305250883605652677639242794753995315199229112894647269426499088162253680340518114657361569012095908691691924534414360438924914998");
byte[] ba = bi.toByteArray();
for (int i = 0; i < ba.length; i++) {
    System.out.print(ba[i] + " ");
}
// 77 97 114 99 111 32 82 111 115 115 105 44 32 50 49 47 48 56 47 49 57 57 52 44 
// 32 69 118 101 110 116 111 32 86 101 103 108 105 111 110 101 32 80 97 115 113 117 
// 97 108 101 44 32 69 118 105 116 97 32 67 108 117 98 44 32 103 105 111 114 110 111 
// 32 50 49 47 48 52 47 50 48 49 54 

If you want a Java byte[] to hold the bytes in the same order as C#, you will need to reverse its contents.

You can just write a simple helper method that reverses the order of the bytes in the array:

public static void reverse(byte[] array) {
    for (int i = 0, j = array.length - 1; i < j; i++, j--) {
        byte b = array[i];
        array[i] = array[j];
        array[j] = b;
    }
}

Or if you have access to 3rd-party libraries, you can use org.apache.commons.lang3.ArrayUtils.reverse(byte[]).

That would give you a byte[] in Java holding the bytes in the same order as C#:

Java

BigInteger bi = new BigInteger("1379080579050447841330186236235223160927998000398161138225875482305250883605652677639242794753995315199229112894647269426499088162253680340518114657361569012095908691691924534414360438924914998");
byte[] ba = bi.toByteArray();
reverse(ba);
for (int i = 0; i < ba.length; i++) {
    System.out.print(ba[i] + " ");
}
// 54 49 48 50 47 52 48 47 49 50 32 111 110 114 111 105 103 32 44 98 117 108 67 
// 32 97 116 105 118 69 32 44 101 108 97 117 113 115 97 80 32 101 110 111 105 108 
// 103 101 86 32 111 116 110 101 118 69 32 44 52 57 57 49 47 56 48 47 49 50 32 44 
// 105 115 115 111 82 32 111 99 114 97 77 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.