Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.pqc.math.linearalgebra;

This is a utility class containing data type conversions using little-endian byte order.

  
  public final class LittleEndianConversions
 {

    
Default constructor (private).
 
     private LittleEndianConversions()
     {
         // empty
     }

    
Convert an octet string of length 4 to an integer. No length checking is performed.

Parameters:
input the byte array holding the octet string
Returns:
an integer representing the octet string input
Throws:
java.lang.ArithmeticException if the length of the given octet string is larger than 4.
 
     public static int OS2IP(byte[] input)
     {
         return ((input[0] & 0xff)) | ((input[1] & 0xff) << 8)
             | ((input[2] & 0xff) << 16) | ((input[3] & 0xff)) << 24;
     }

    
Convert an byte array of length 4 beginning at offset into an integer.

Parameters:
input the byte array
inOff the offset into the byte array
Returns:
the resulting integer
 
     public static int OS2IP(byte[] inputint inOff)
     {
         int result = input[inOff++] & 0xff;
         result |= (input[inOff++] & 0xff) << 8;
         result |= (input[inOff++] & 0xff) << 16;
         result |= (input[inOff] & 0xff) << 24;
         return result;
     }

    
Convert a byte array of the given length beginning at offset into an integer.

Parameters:
input the byte array
inOff the offset into the byte array
inLen the length of the encoding
Returns:
the resulting integer
 
     public static int OS2IP(byte[] inputint inOffint inLen)
     {
         int result = 0;
         for (int i = inLen - 1; i >= 0; i--)
         {
             result |= (input[inOff + i] & 0xff) << (8 * i);
         }
         return result;
     }

    
Convert a byte array of length 8 beginning at inOff into a long integer.

Parameters:
input the byte array
inOff the offset into the byte array
Returns:
the resulting long integer
 
     public static long OS2LIP(byte[] inputint inOff)
     {
         long result = input[inOff++] & 0xff;
         result |= (input[inOff++] & 0xff) << 8;
         result |= (input[inOff++] & 0xff) << 16;
         result |= ((long)input[inOff++] & 0xff) << 24;
         result |= ((long)input[inOff++] & 0xff) << 32;
         result |= ((long)input[inOff++] & 0xff) << 40;
         result |= ((long)input[inOff++] & 0xff) << 48;
         result |= ((long)input[inOff++] & 0xff) << 56;
         return result;
     }

    
Convert an integer to an octet string of length 4.

Parameters:
x the integer to convert
Returns:
the converted integer
 
     public static byte[] I2OSP(int x)
     {
         byte[] result = new byte[4];
        result[0] = (byte)x;
        result[1] = (byte)(x >>> 8);
        result[2] = (byte)(x >>> 16);
        result[3] = (byte)(x >>> 24);
        return result;
    }

    
Convert an integer into a byte array beginning at the specified offset.

Parameters:
value the integer to convert
output the byte array to hold the result
outOff the integer offset into the byte array
    public static void I2OSP(int valuebyte[] outputint outOff)
    {
        output[outOff++] = (byte)value;
        output[outOff++] = (byte)(value >>> 8);
        output[outOff++] = (byte)(value >>> 16);
        output[outOff++] = (byte)(value >>> 24);
    }

    
Convert an integer to a byte array beginning at the specified offset. No length checking is performed (i.e., if the integer cannot be encoded with length octets, it is truncated).

Parameters:
value the integer to convert
output the byte array to hold the result
outOff the integer offset into the byte array
outLen the length of the encoding
    public static void I2OSP(int valuebyte[] outputint outOffint outLen)
    {
        for (int i = outLen - 1; i >= 0; i--)
        {
            output[outOff + i] = (byte)(value >>> (8 * i));
        }
    }

    
Convert an integer to a byte array of length 8.

Parameters:
input the integer to convert
Returns:
the converted integer
    public static byte[] I2OSP(long input)
    {
        byte[] output = new byte[8];
        output[0] = (byte)input;
        output[1] = (byte)(input >>> 8);
        output[2] = (byte)(input >>> 16);
        output[3] = (byte)(input >>> 24);
        output[4] = (byte)(input >>> 32);
        output[5] = (byte)(input >>> 40);
        output[6] = (byte)(input >>> 48);
        output[7] = (byte)(input >>> 56);
        return output;
    }

    
Convert an integer to a byte array of length 8.

Parameters:
input the integer to convert
output byte array holding the output
outOff offset in output array where the result is stored
    public static void I2OSP(long inputbyte[] outputint outOff)
    {
        output[outOff++] = (byte)input;
        output[outOff++] = (byte)(input >>> 8);
        output[outOff++] = (byte)(input >>> 16);
        output[outOff++] = (byte)(input >>> 24);
        output[outOff++] = (byte)(input >>> 32);
        output[outOff++] = (byte)(input >>> 40);
        output[outOff++] = (byte)(input >>> 48);
        output[outOff] = (byte)(input >>> 56);
    }

    
Convert an int array to a byte array of the specified length. No length checking is performed (i.e., if the last integer cannot be encoded with length % 4 octets, it is truncated).

Parameters:
input the int array
outLen the length of the converted array
Returns:
the converted array
    public static byte[] toByteArray(int[] inputint outLen)
    {
        int intLen = input.length;
        byte[] result = new byte[outLen];
        int index = 0;
        for (int i = 0; i <= intLen - 2; i++, index += 4)
        {
            I2OSP(input[i], resultindex);
        }
        I2OSP(input[intLen - 1], resultindexoutLen - index);
        return result;
    }

    
Convert a byte array to an int array.

Parameters:
input the byte array
Returns:
the converted array
    public static int[] toIntArray(byte[] input)
    {
        int intLen = (input.length + 3) / 4;
        int lastLen = input.length & 0x03;
        int[] result = new int[intLen];
        int index = 0;
        for (int i = 0; i <= intLen - 2; i++, index += 4)
        {
            result[i] = OS2IP(inputindex);
        }
        if (lastLen != 0)
        {
            result[intLen - 1] = OS2IP(inputindexlastLen);
        }
        else
        {
            result[intLen - 1] = OS2IP(inputindex);
        }
        return result;
    }
New to GrepCode? Check out our FAQ X