<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Java</title><link>http://www.ralbu.com:80/tags/java</link><description>Java</description><item><title>BigInteger in Java and C#</title><link>http://www.ralbu.com:80/post/2013/07/19/BigInteger-in-Java-and-C</link><description>&lt;p&gt;I was working on a task where I need to generate a BigInteger number in C# which should be the same if generated in Java. Two libraries &amp;ndash; in Java and C# should generate BigInteger and they should be the same.&lt;/p&gt;
&lt;p&gt;BigInteger in Java and C# are different so you can't use something like this&lt;/p&gt;
&lt;pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;java.math.BigInteger(byte[])

System.Numerics.BigInteger(byte[]):&lt;/pre&gt;
&lt;p&gt;For Java&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Translates a byte array containing the two's-complement binary representation of a BigInteger into a &lt;br /&gt;BigInteger. The input array is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;And C#&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The individual bytes in the value array should be in little-endian order, from lowest-order byte to highest-order byte&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;First of all what is big-endian and little-endian? These are terms that describe the order in which a sequence of bytes are stored in the computer memory. Big-endian is an order in which the most significant value in the sequence is stored first, at the lowest storage address. In little-endian the less significant value in the sequence is store first.&lt;/p&gt;
&lt;p&gt;For example, in a big-endian system the hexadecimal 3A86 will be stored as 3A86. If 3A is stored at the address 1000 then 86 will be stored at the address 1001. In a little-endian computer the same number will be stored as 863A &amp;ndash; 86 at address 1000 and 3A at address 1001.&lt;/p&gt;
&lt;p&gt;The same number in binary format&lt;/p&gt;
&lt;p&gt;00000000 00000000 00111010 10000110&lt;/p&gt;
&lt;p&gt;Will be stored as follows&lt;/p&gt;
&lt;table style="width: 400px;" cellpadding="2" cellspacing="0" border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" width="133"&gt;&lt;strong&gt;Address&lt;/strong&gt;&lt;/td&gt;
&lt;td valign="top" width="133"&gt;&lt;strong&gt;Big-endian&lt;/strong&gt;&lt;/td&gt;
&lt;td valign="top" width="133"&gt;&lt;strong&gt;Little-endian&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="133"&gt;1000&lt;/td&gt;
&lt;td valign="top" width="133"&gt;00000000&lt;/td&gt;
&lt;td valign="top" width="133"&gt;10000110&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="133"&gt;1001&lt;/td&gt;
&lt;td valign="top" width="133"&gt;00000000&lt;/td&gt;
&lt;td valign="top" width="133"&gt;00111010&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="133"&gt;1002&lt;/td&gt;
&lt;td valign="top" width="133"&gt;00111010&lt;/td&gt;
&lt;td valign="top" width="133"&gt;00000000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top" width="133"&gt;1003&lt;/td&gt;
&lt;td valign="top" width="133"&gt;10000110&lt;/td&gt;
&lt;td valign="top" width="133"&gt;00000000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;What we need to do is provide the byte array in big-endian so the behaviour will be the same as in Java.&lt;/p&gt;
&lt;p&gt;If we have variable data&lt;/p&gt;
&lt;pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;byte[] data = {1, 65, 30, 47};&lt;/pre&gt;
&lt;p&gt;Will transform into big-endian by reversing bytes.&lt;/p&gt;
&lt;pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;byte[] reverseData = data.Reverse().ToArray();&lt;/pre&gt;
&lt;p&gt;The output array will be&lt;/p&gt;
&lt;p&gt;{47, 30, 65, 1}&lt;/p&gt;
&lt;p&gt;and provide it to the BigInteger constructor&lt;/p&gt;
&lt;pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;new BigInteger(reverseData);&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;NOTE&lt;/p&gt;
&lt;p&gt;If you try to compare the variable data in Java and C# (when you get the value from another operation, file load, hashing, etc.) by printing them, then they might be different. The reason for this is that Java bytes are signed but C# bytes are unsigned. A Java byte equivalent to C# is sbyte.&lt;/p&gt;
&lt;p&gt;This conversion will do the trick&lt;/p&gt;
&lt;pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;sbyte[] dataCnv = (sbyte[]) (Array) data;&lt;/pre&gt;</description><pubDate>Fri, 19 Jul 2013 14:35:00 GMT</pubDate><guid isPermaLink="true">http://www.ralbu.com:80/post/2013/07/19/BigInteger-in-Java-and-C</guid></item></channel></rss>