Ok so I am porting some java code
https://github.com/pmarches/jRippleAPI
to my c# application
https://github.com/jargoman/ripple-client-gtk
Note that the original code is gpl but I have special licensing agreement from the author. This code will be semi proprietary, dual license probably.
I'm now stuck at a function that at first glance I thought would be simple. It suddenly occured to me that endianness is a factor due to hashing and digital signatures. This is a financial trading application.
Here is the java code
The problem is my code uses the bigInteger type built into c# which is little endian and java's bigInterger is bigEndian. Also c#'s bigInteger does not contain the function toByteArray().
The line if((biToConvert.bitLength()+7)/8!=twosComplement.length){ is confusing me because although I understand that the reasonong has something to do with an extra bit (byte?) due to the sign. I don't quite understand how I can port this to little endian.
If someone where to port this function for me most likely the code would be self explanatory and I could reuse the concept throughout the rest of my code.
Ps. Anyone interested in developing a virtual currency application or is interested in my next project which is a virtual financial trading application feel free to pm me.
https://github.com/pmarches/jRippleAPI
to my c# application
https://github.com/jargoman/ripple-client-gtk
Note that the original code is gpl but I have special licensing agreement from the author. This code will be semi proprietary, dual license probably.
I'm now stuck at a function that at first glance I thought would be simple. It suddenly occured to me that endianness is a factor due to hashing and digital signatures. This is a financial trading application.
Here is the java code
C#:
public static byte[] bigIntegerToBytes(BigInteger biToConvert, int nbBytesToReturn)
{
//toArray will return the minimum number of bytes required to encode the biginteger in two's complement.
//Could be less than the expected number of bytes
byte[] twosComplement = biToConvert.toByteArray();
byte[] bytesToReturn=new byte[nbBytesToReturn];
if((biToConvert.bitLength()+7)/8!=twosComplement.length){
//Two's complement representation has a sign bit set on the most significant byte
byte[] twosComplementWithoutSign = new byte[twosComplement.length-1];
System.arraycopy(twosComplement, 1, twosComplementWithoutSign, 0, twosComplementWithoutSign.length);
twosComplement=twosComplementWithoutSign;
}
int nbBytesOfPaddingRequired=nbBytesToReturn-twosComplement.length;
if(nbBytesOfPaddingRequired<0){
throw new RuntimeException("nbBytesToReturn "+nbBytesToReturn+" is too small");
}
System.arraycopy(twosComplement, 0, bytesToReturn, nbBytesOfPaddingRequired, twosComplement.length);
return bytesToReturn;
}
The problem is my code uses the bigInteger type built into c# which is little endian and java's bigInterger is bigEndian. Also c#'s bigInteger does not contain the function toByteArray().
The line if((biToConvert.bitLength()+7)/8!=twosComplement.length){ is confusing me because although I understand that the reasonong has something to do with an extra bit (byte?) due to the sign. I don't quite understand how I can port this to little endian.
If someone where to port this function for me most likely the code would be self explanatory and I could reuse the concept throughout the rest of my code.
Ps. Anyone interested in developing a virtual currency application or is interested in my next project which is a virtual financial trading application feel free to pm me.