edu.neu.ccs.util
Class Conversions

java.lang.Object
  extended byedu.neu.ccs.util.Conversions

public class Conversions
extends Object

Class Conversions contains type conversion utility functions suitable for processing byte sequences and byte arrays and for conversion of integral values (short, int, or long) to floating values (float or double) that are between -1 and +1.

Class Conversions cannot be instantiated.

Since:
1.0
Version:
2.3

Constructor Summary
private Conversions()
          Private constructor to prevent instantiation.
 
Method Summary
static int bytesToIntHiLo(byte[] b)
          Returns the int constructed from the byte array assuming that the byte order is high-to-low.
static int bytesToIntHiLo(byte[] b, int start)
          Returns the int constructed from the byte array starting at the given start index and assuming that the byte order is high-to-low.
static int bytesToIntHiLo(byte b0, byte b1, byte b2, byte b3)
          Returns the int constructed from the four given bytes assuming the byte order is high-to-low.
static int bytesToIntLoHi(byte[] b)
          Returns the int constructed from the byte array assuming that the byte order is low-to-high.
static int bytesToIntLoHi(byte[] b, int start)
          Returns the int constructed from the byte array starting at the given start index and assuming that the byte order is low-to-high.
static int bytesToIntLoHi(byte b0, byte b1, byte b2, byte b3)
          Returns the int constructed from the four given bytes assuming the byte order is low-to-high.
static long bytesToLongHiLo(byte[] b)
          Returns the long constructed from the byte array assuming that the byte order is high-to-low.
static long bytesToLongHiLo(byte[] b, int start)
          Returns the long constructed from the byte array starting at the given start index and assuming that the byte order is high-to-low.
static long bytesToLongHiLo(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7)
          Returns the long constructed from the eight given bytes assuming the byte order is high-to-low.
static long bytesToLongLoHi(byte[] b)
          Returns the long constructed from the byte array assuming that the byte order is low-to-high.
static long bytesToLongLoHi(byte[] b, int start)
          Returns the long constructed from the byte array starting at the given start index and assuming that the byte order is low-to-high.
static long bytesToLongLoHi(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7)
          Returns the long constructed from the eight given bytes assuming the byte order is low-to-high.
static short bytesToShortHiLo(byte[] b)
          Returns the short constructed from the byte array assuming that the byte order is high-to-low.
static short bytesToShortHiLo(byte[] b, int start)
          Returns the short constructed from the byte array starting at the given start index and assuming that the byte order is high-to-low.
static short bytesToShortHiLo(byte b0, byte b1)
          Returns the short constructed from the pair of bytes assuming b0 is the high byte and b1 is the low byte.
static short bytesToShortLoHi(byte[] b)
          Returns the short constructed from the byte array assuming that the byte order is low-to-high.
static short bytesToShortLoHi(byte[] b, int start)
          Returns the short constructed from the byte array starting at the given start index and assuming that the byte order is low-to-high.
static short bytesToShortLoHi(byte b0, byte b1)
          Returns the short constructed from the pair of bytes assuming b0 is the low byte and b1 is the high byte.
static int doubleToInt(double x)
          If the given double x is between -1 and +1, then returns the value x * Integer.MAX_VALUE rounded to the nearest int.
static long doubleToLong(double x)
          If the given double x is between -1 and +1, then returns the value x * Long.MAX_VALUE rounded to the nearest long.
static short doubleToShort(double x)
          If the given double x is between -1 and +1, then returns the value x * Short.MAX_VALUE rounded to the nearest short.
static int floatToInt(float x)
          If the given float x is between -1 and +1, then returns the value x * Integer.MAX_VALUE rounded to the nearest int.
static long floatToLong(float x)
          If the given float x is between -1 and +1, then returns the value x * Long.MAX_VALUE rounded to the nearest long.
static short floatToShort(float x)
          If the given float x is between -1 and +1, then returns the value x * Short.MAX_VALUE rounded to the nearest short.
static byte[] intToBytesHiLo(int s)
          Returns an array of bytes of size 4 with the 4 bytes of the given int in high-to-low order.
static byte[] intToBytesLoHi(int s)
          Returns an array of bytes of size 4 with the 4 bytes of the given int in low-to-high order.
static double intToDouble(int s)
          Returns the double between -1 and +1 calculated by dividing the given int by Integer.MAX_VALUE.
static float intToFloat(int s)
          Returns the float between -1 and +1 calculated by dividing the given int by Integer.MAX_VALUE.
static byte[] longToBytesHiLo(long s)
          Returns an array of bytes of size 8 with the 8 bytes of the given long in high-to-low order.
static byte[] longToBytesLoHi(long s)
          Returns an array of bytes of size 8 with the 8 bytes of the given long in low-to-high order.
static double longToDouble(long s)
          Returns the double between -1 and +1 calculated by dividing the given long by Long.MAX_VALUE.
static float longToFloat(long s)
          Returns the float between -1 and +1 calculated by dividing the given long by Long.MAX_VALUE.
static byte[] shortToBytesHiLo(short s)
          Returns an array of bytes of size 2 with the 2 bytes of the given short in high-to-low order.
static byte[] shortToBytesLoHi(short s)
          Returns an array of bytes of size 2 with the 2 bytes of the given short in low-to-high order.
static double shortToDouble(short s)
          Returns the double between -1 and +1 calculated by dividing the given short by Short.MAX_VALUE.
static float shortToFloat(short s)
          Returns the float between -1 and +1 calculated by dividing the given short by Short.MAX_VALUE.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Conversions

private Conversions()
Private constructor to prevent instantiation.

Method Detail

bytesToShortLoHi

public static short bytesToShortLoHi(byte[] b)

Returns the short constructed from the byte array assuming that the byte order is low-to-high.

If the byte array is null or has length 0, return 0.

If the byte array does not have enough items then assume the missing items are 0.

Ignore items in the byte array that are not in the range: 0 to 1.

Parameters:
b - the byte array
Returns:
the short constructed from the byte array
See Also:
bytesToShortLoHi(byte[], int), bytesToShortLoHi(byte, byte), bytesToShortHiLo(byte[]), bytesToShortHiLo(byte[], int), bytesToShortHiLo(byte, byte)

bytesToShortLoHi

public static short bytesToShortLoHi(byte[] b,
                                     int start)

Returns the short constructed from the byte array starting at the given start index and assuming that the byte order is low-to-high.

If the byte array is null or start is not a valid index into the byte array, return 0.

If the byte array does not have enough items from start onward then assume the missing items are 0.

Ignore items in the byte array that are not in the range: start to (start + 1).

Parameters:
b - the byte array
start - the start index
Returns:
the short constructed from the byte array
See Also:
bytesToShortLoHi(byte[]), bytesToShortLoHi(byte, byte), bytesToShortHiLo(byte[]), bytesToShortHiLo(byte[], int), bytesToShortHiLo(byte, byte)

bytesToShortLoHi

public static short bytesToShortLoHi(byte b0,
                                     byte b1)

Returns the short constructed from the pair of bytes assuming b0 is the low byte and b1 is the high byte.

Parameters:
b0 - the low byte
b1 - the high byte
Returns:
the short constructed from b0 and b1
See Also:
bytesToShortLoHi(byte[]), bytesToShortLoHi(byte[], int), bytesToShortHiLo(byte[]), bytesToShortHiLo(byte[], int), bytesToShortHiLo(byte, byte)

bytesToShortHiLo

public static short bytesToShortHiLo(byte[] b)

Returns the short constructed from the byte array assuming that the byte order is high-to-low.

If the byte array is null or has length 0, return 0.

If the byte array does not have enough items then assume the missing items are 0.

Ignore items in the byte array that are not in the range: 0 to 1.

Parameters:
b - the byte array
Returns:
the short constructed from the byte array
See Also:
bytesToShortLoHi(byte[]), bytesToShortLoHi(byte[], int), bytesToShortLoHi(byte, byte), bytesToShortHiLo(byte[], int), bytesToShortHiLo(byte, byte)

bytesToShortHiLo

public static short bytesToShortHiLo(byte[] b,
                                     int start)

Returns the short constructed from the byte array starting at the given start index and assuming that the byte order is high-to-low.

If the byte array is null or start is not a valid index into the byte array, return 0.

If the byte array does not have enough items from start onward then assume the missing items are 0.

Ignore items in the byte array that are not in the range: start to (start + 1).

Parameters:
b - the byte array
start - the start index
Returns:
the short constructed from the byte array
See Also:
bytesToShortLoHi(byte[]), bytesToShortLoHi(byte[], int), bytesToShortLoHi(byte, byte), bytesToShortHiLo(byte[]), bytesToShortHiLo(byte, byte)

bytesToShortHiLo

public static short bytesToShortHiLo(byte b0,
                                     byte b1)

Returns the short constructed from the pair of bytes assuming b0 is the high byte and b1 is the low byte.

Parameters:
b0 - the high byte
b1 - the low byte
Returns:
the short constructed from b0 and b1
See Also:
bytesToShortLoHi(byte[]), bytesToShortLoHi(byte[], int), bytesToShortLoHi(byte, byte), bytesToShortHiLo(byte[]), bytesToShortHiLo(byte[], int)

shortToBytesLoHi

public static byte[] shortToBytesLoHi(short s)

Returns an array of bytes of size 2 with the 2 bytes of the given short in low-to-high order.

Parameters:
s - the short that defines the bytes
Returns:
the associated byte array
See Also:
shortToBytesHiLo(short)

shortToBytesHiLo

public static byte[] shortToBytesHiLo(short s)

Returns an array of bytes of size 2 with the 2 bytes of the given short in high-to-low order.

Parameters:
s - the short that defines the bytes
Returns:
the associated byte array
See Also:
shortToBytesLoHi(short)

bytesToIntLoHi

public static int bytesToIntLoHi(byte[] b)

Returns the int constructed from the byte array assuming that the byte order is low-to-high.

If the byte array is null or has length 0, return 0.

If the byte array does not have enough items then assume the missing items are 0.

Ignore items in the byte array that are not in the range: 0 to 3.

Parameters:
b - the byte array
Returns:
the int constructed from the byte array
See Also:
bytesToIntLoHi(byte[], int), bytesToIntLoHi(byte, byte, byte, byte), bytesToIntHiLo(byte[]), bytesToIntHiLo(byte[], int), bytesToIntHiLo(byte, byte, byte, byte)

bytesToIntLoHi

public static int bytesToIntLoHi(byte[] b,
                                 int start)

Returns the int constructed from the byte array starting at the given start index and assuming that the byte order is low-to-high.

If the byte array is null or start is not a valid index into the byte array, return 0.

If the byte array does not have enough items from start onward then assume the missing items are 0.

Ignore items in the byte array that are not in the range: start to (start + 3).

Parameters:
b - the byte array
start - the start index
Returns:
the int constructed from the byte array
See Also:
bytesToIntLoHi(byte[]), bytesToIntLoHi(byte, byte, byte, byte), bytesToIntHiLo(byte[]), bytesToIntHiLo(byte[], int), bytesToIntHiLo(byte, byte, byte, byte)

bytesToIntLoHi

public static int bytesToIntLoHi(byte b0,
                                 byte b1,
                                 byte b2,
                                 byte b3)

Returns the int constructed from the four given bytes assuming the byte order is low-to-high.

Parameters:
b0 - the lowest byte
b1 - the next byte
b2 - the next byte
b3 - the next byte
Returns:
the int constructed from the four bytes
See Also:
bytesToIntLoHi(byte[]), bytesToIntLoHi(byte[], int), bytesToIntHiLo(byte[]), bytesToIntHiLo(byte[], int), bytesToIntHiLo(byte, byte, byte, byte)

bytesToIntHiLo

public static int bytesToIntHiLo(byte[] b)

Returns the int constructed from the byte array assuming that the byte order is high-to-low.

If the byte array is null or has length 0, return 0.

If the byte array does not have enough items then assume the missing items are 0.

Ignore items in the byte array that are not in the range: 0 to 3.

Parameters:
b - the byte array
Returns:
the int constructed from the byte array
See Also:
bytesToIntLoHi(byte[]), bytesToIntLoHi(byte[], int), bytesToIntLoHi(byte, byte, byte, byte), bytesToIntHiLo(byte[], int), bytesToIntHiLo(byte, byte, byte, byte)

bytesToIntHiLo

public static int bytesToIntHiLo(byte[] b,
                                 int start)

Returns the int constructed from the byte array starting at the given start index and assuming that the byte order is high-to-low.

If the byte array is null or start is not a valid index into the byte array, return 0.

If the byte array does not have enough items from start onward then assume the missing items are 0.

Ignore items in the byte array that are not in the range: start to (start + 3).

Parameters:
b - the byte array
start - the start index
Returns:
the int constructed from the byte array
See Also:
bytesToIntLoHi(byte[]), bytesToIntLoHi(byte[], int), bytesToIntLoHi(byte, byte, byte, byte), bytesToIntHiLo(byte[]), bytesToIntHiLo(byte, byte, byte, byte)

bytesToIntHiLo

public static int bytesToIntHiLo(byte b0,
                                 byte b1,
                                 byte b2,
                                 byte b3)

Returns the int constructed from the four given bytes assuming the byte order is high-to-low.

Parameters:
b0 - the highest byte
b1 - the next byte
b2 - the next byte
b3 - the next byte
Returns:
the int constructed from the four bytes
See Also:
bytesToIntLoHi(byte[]), bytesToIntLoHi(byte[], int), bytesToIntLoHi(byte, byte, byte, byte), bytesToIntHiLo(byte[]), bytesToIntHiLo(byte[], int)

intToBytesLoHi

public static byte[] intToBytesLoHi(int s)

Returns an array of bytes of size 4 with the 4 bytes of the given int in low-to-high order.

Parameters:
s - the int that defines the bytes
Returns:
the associated byte array
See Also:
intToBytesHiLo(int)

intToBytesHiLo

public static byte[] intToBytesHiLo(int s)

Returns an array of bytes of size 4 with the 4 bytes of the given int in high-to-low order.

Parameters:
s - the int that defines the bytes
Returns:
the associated byte array
See Also:
intToBytesLoHi(int)

bytesToLongLoHi

public static long bytesToLongLoHi(byte[] b)

Returns the long constructed from the byte array assuming that the byte order is low-to-high.

If the byte array is null or has length 0, return 0.

If the byte array does not have enough items then assume the missing items are 0.

Ignore items in the byte array that are not in the range: 0 to 7.

Parameters:
b - the byte array
Returns:
the long constructed from the byte array
See Also:
bytesToLongLoHi(byte[], int), bytesToLongLoHi(byte, byte, byte, byte, byte, byte, byte, byte), bytesToLongHiLo(byte[]), bytesToLongHiLo(byte[], int), bytesToLongHiLo(byte, byte, byte, byte, byte, byte, byte, byte)

bytesToLongLoHi

public static long bytesToLongLoHi(byte[] b,
                                   int start)

Returns the long constructed from the byte array starting at the given start index and assuming that the byte order is low-to-high.

If the byte array is null or start is not a valid index into the byte array, return 0.

If the byte array does not have enough items from start onward then assume the missing items are 0.

Ignore items in the byte array that are not in the range: start to (start + 7).

Parameters:
b - the byte array
start - the start index
Returns:
the long constructed from the byte array
See Also:
bytesToLongLoHi(byte[]), bytesToLongLoHi(byte, byte, byte, byte, byte, byte, byte, byte), bytesToLongHiLo(byte[]), bytesToLongHiLo(byte[], int), bytesToLongHiLo(byte, byte, byte, byte, byte, byte, byte, byte)

bytesToLongLoHi

public static long bytesToLongLoHi(byte b0,
                                   byte b1,
                                   byte b2,
                                   byte b3,
                                   byte b4,
                                   byte b5,
                                   byte b6,
                                   byte b7)

Returns the long constructed from the eight given bytes assuming the byte order is low-to-high.

Parameters:
b0 - the lowest byte
b1 - the next byte
b2 - the next byte
b3 - the next byte
b4 - the next byte
b5 - the next byte
b6 - the next byte
b7 - the next byte
Returns:
the long constructed from the eight bytes
See Also:
bytesToLongLoHi(byte[]), bytesToLongLoHi(byte[], int), bytesToLongHiLo(byte[]), bytesToLongHiLo(byte[], int), bytesToLongHiLo(byte, byte, byte, byte, byte, byte, byte, byte)

bytesToLongHiLo

public static long bytesToLongHiLo(byte[] b)

Returns the long constructed from the byte array assuming that the byte order is high-to-low.

If the byte array is null or has length 0, return 0.

If the byte array does not have enough items then assume the missing items are 0.

Ignore items in the byte array that are not in the range: 0 to 7.

Parameters:
b - the byte array
Returns:
the long constructed from the byte array
See Also:
bytesToLongLoHi(byte[]), bytesToLongLoHi(byte[], int), bytesToLongLoHi(byte, byte, byte, byte, byte, byte, byte, byte), bytesToLongHiLo(byte[], int), bytesToLongHiLo(byte, byte, byte, byte, byte, byte, byte, byte)

bytesToLongHiLo

public static long bytesToLongHiLo(byte[] b,
                                   int start)

Returns the long constructed from the byte array starting at the given start index and assuming that the byte order is high-to-low.

If the byte array is null or start is not a valid index into the byte array, return 0.

If the byte array does not have enough items from start onward then assume the missing items are 0.

Ignore items in the byte array that are not in the range: start to (start + 7).

Parameters:
b - the byte array
start - the start index
Returns:
the long constructed from the byte array
See Also:
bytesToLongLoHi(byte[]), bytesToLongLoHi(byte[], int), bytesToLongLoHi(byte, byte, byte, byte, byte, byte, byte, byte), bytesToLongHiLo(byte[]), bytesToLongHiLo(byte, byte, byte, byte, byte, byte, byte, byte)

bytesToLongHiLo

public static long bytesToLongHiLo(byte b0,
                                   byte b1,
                                   byte b2,
                                   byte b3,
                                   byte b4,
                                   byte b5,
                                   byte b6,
                                   byte b7)

Returns the long constructed from the eight given bytes assuming the byte order is high-to-low.

Parameters:
b0 - the highest byte
b1 - the next byte
b2 - the next byte
b3 - the next byte
b4 - the next byte
b5 - the next byte
b6 - the next byte
b7 - the next byte
Returns:
the long constructed from the eight bytes
See Also:
bytesToLongLoHi(byte[]), bytesToLongLoHi(byte[], int), bytesToLongLoHi(byte, byte, byte, byte, byte, byte, byte, byte), bytesToLongHiLo(byte[]), bytesToLongHiLo(byte[], int)

longToBytesLoHi

public static byte[] longToBytesLoHi(long s)

Returns an array of bytes of size 8 with the 8 bytes of the given long in low-to-high order.

Parameters:
s - the long that defines the bytes
Returns:
the associated byte array
See Also:
longToBytesHiLo(long)

longToBytesHiLo

public static byte[] longToBytesHiLo(long s)

Returns an array of bytes of size 8 with the 8 bytes of the given long in high-to-low order.

Parameters:
s - the long that defines the bytes
Returns:
the associated byte array
See Also:
longToBytesLoHi(long)

shortToFloat

public static float shortToFloat(short s)

Returns the float between -1 and +1 calculated by dividing the given short by Short.MAX_VALUE.

The short Short.MIN_VALUE is treated as a special case. For this value, the method returns -1.

Parameters:
s - the short to convert to a float between -1 and +1
Returns:
the resulting float

intToFloat

public static float intToFloat(int s)

Returns the float between -1 and +1 calculated by dividing the given int by Integer.MAX_VALUE.

The int Integer.MIN_VALUE is treated as a special case. For this value, the method returns -1.

The method intends to map an int to a float between -1 and +1. Its value is quite different from that of the following method in class Float:

Parameters:
s - the int to convert to a float between -1 and +1
Returns:
the resulting float
See Also:
Float.intBitsToFloat(int)

longToFloat

public static float longToFloat(long s)

Returns the float between -1 and +1 calculated by dividing the given long by Long.MAX_VALUE.

The long Long.MIN_VALUE is treated as a special case. For this value, the method returns -1.

Parameters:
s - the long to convert to a float between -1 and +1
Returns:
the resulting float

floatToShort

public static short floatToShort(float x)

If the given float x is between -1 and +1, then returns the value x * Short.MAX_VALUE rounded to the nearest short.

If the given float x is greater than or equal to +1, then returns Short.MAX_VALUE.

If the given float x is less than or equal to -1, then returns -Short.MAX_VALUE.

If the given float is NaN, returns 0.

Parameters:
x - the float between -1 and +1 to convert to a short
Returns:
the resulting short

floatToInt

public static int floatToInt(float x)

If the given float x is between -1 and +1, then returns the value x * Integer.MAX_VALUE rounded to the nearest int.

If the given float x is greater than or equal to +1, then returns Integer.MAX_VALUE.

If the given float x is less than or equal to -1, then returns -Integer.MAX_VALUE.

If the given float is NaN, returns 0.

The method intends to map a float between -1 and +1 to an int. Its value is quite different from that of the following methods in class Float:

Parameters:
x - the float between -1 and +1 to convert to an int
Returns:
the resulting int
See Also:
Float.floatToIntBits(float), Float.floatToRawIntBits(float)

floatToLong

public static long floatToLong(float x)

If the given float x is between -1 and +1, then returns the value x * Long.MAX_VALUE rounded to the nearest long.

If the given float x is greater than or equal to +1, then returns Long.MAX_VALUE.

If the given float x is less than or equal to -1, then returns -Long.MAX_VALUE.

If the given float is NaN, returns 0.

Parameters:
x - the float between -1 and +1 to convert to a long
Returns:
the resulting long

shortToDouble

public static double shortToDouble(short s)

Returns the double between -1 and +1 calculated by dividing the given short by Short.MAX_VALUE.

The short Short.MIN_VALUE is treated as a special case. For this value, the method returns -1.

Parameters:
s - the short to convert to a double between -1 and +1
Returns:
the resulting double

intToDouble

public static double intToDouble(int s)

Returns the double between -1 and +1 calculated by dividing the given int by Integer.MAX_VALUE.

The int Integer.MIN_VALUE is treated as a special case. For this value, the method returns -1.

Parameters:
s - the int to convert to a double between -1 and +1
Returns:
the resulting double

longToDouble

public static double longToDouble(long s)

Returns the double between -1 and +1 calculated by dividing the given long by Long.MAX_VALUE.

The long Long.MIN_VALUE is treated as a special case. For this value, the method returns -1.

The method intends to map a long to a double between -1 and +1. Its value is quite different from that of the following method in class Double:

Parameters:
s - the long to convert to a double between -1 and +1
Returns:
the resulting double
See Also:
Double.longBitsToDouble(long)

doubleToShort

public static short doubleToShort(double x)

If the given double x is between -1 and +1, then returns the value x * Short.MAX_VALUE rounded to the nearest short.

If the given double x is greater than or equal to +1, then returns Short.MAX_VALUE.

If the given double x is less than or equal to -1, then returns -Short.MAX_VALUE.

If the given double is NaN, returns 0.

Parameters:
x - the double between -1 and +1 to convert to a short
Returns:
the resulting short

doubleToInt

public static int doubleToInt(double x)

If the given double x is between -1 and +1, then returns the value x * Integer.MAX_VALUE rounded to the nearest int.

If the given double x is greater than or equal to +1, then returns Integer.MAX_VALUE.

If the given double x is less than or equal to -1, then returns -Integer.MAX_VALUE.

If the given double is NaN, returns 0.

Parameters:
x - the double between -1 and +1 to convert to an int
Returns:
the resulting int

doubleToLong

public static long doubleToLong(double x)

If the given double x is between -1 and +1, then returns the value x * Long.MAX_VALUE rounded to the nearest long.

If the given double x is greater than or equal to +1, then returns Long.MAX_VALUE.

If the given double x is less than or equal to -1, then returns -Long.MAX_VALUE.

If the given double is NaN, returns 0.

The method intends to map a double between -1 and +1 to a long. Its value is quite different from that of the following methods in class Double:

Parameters:
x - the double between -1 and +1 to convert to a long
Returns:
the resulting long
See Also:
Double.doubleToLongBits(double), Double.doubleToRawLongBits(double)