Question Can someone port this function from java to c# - note endianness.

jargoman

Member
Joined
Dec 19, 2013
Messages
7
Programming Experience
3-5
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
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.
 
Well, I came up with the fllowing code. I decided to throw an exception for negative values and hope the code doesn't require the retrieval of big endian negative values.

C#:
public static byte[] bigIntegerToBytes (BigInteger biToConvert, int nbBytesToReturn)
        {
            byte[] twosComplement = BigInteger.ToByteArray ();
            byte[] bytesToReturn = new byte[nbBytesToReturn];
            //TODO two's compliments
            //if ((biToConvert.

            if (biToConvert.Sign < 0) {
                // not sure about this one
                throw new NotImplementedException ("bigIntegerToBytes does not implement the retrieval of negative values");
            }

            // not sure about this either
            if (twosComplement [twosComplement.Length - 1] == 0) {
                // there's an empty byte at the end (little endian) to denote a positive number
                byte[] twosComplementWithoutSign = new byte[twosComplement.Length - 1];
                System.Array.Copy (twosComplement, twosComplementWithoutSign, twosComplementWithoutSign.Length);
                twosComplement = twosComplementWithoutSign;
            }

            int nbBytesOfPaddingRequired = nbBytesToReturn - twosComplement.Length;

            if (nbBytesOfPaddingRequired < 0) {
                throw new IndexOutOfRangeException("nbBytesToReturn "+nbBytesToReturn+" is too small");
            }

            // big endian
            //System.Array.Copy(twosComplement, 0, bytesToReturn, nbBytesOfPaddingRequired, twosComplement.Length);

            // little endian
            System.Array.Copy(twosComplement, bytesToReturn, twosComplement.Length);

            return bytesToReturn;

        }
 
Back
Top Bottom