namespace MiscUtil.Conversion
{
///
/// Implementation of EndianBitConverter which converts to/from little-endian
/// byte arrays.
///
public sealed class LittleEndianBitConverter : EndianBitConverter
{
///
/// Indicates the byte order ("endianess") in which data is converted using this class.
///
///
/// Different computer architectures store data using different byte orders. "Big-endian"
/// means the most significant byte is on the left end of a word. "Little-endian" means the
/// most significant byte is on the right end of a word.
///
/// true if this converter is little-endian, false otherwise.
public sealed override bool IsLittleEndian()
{
return true;
}
///
/// Indicates the byte order ("endianess") in which data is converted using this class.
///
public sealed override Endianness Endianness
{
get { return Endianness.LittleEndian; }
}
///
/// Copies the specified number of bytes from value to buffer, starting at index.
///
/// The value to copy
/// The number of bytes to copy
/// The buffer to copy the bytes into
/// The index to start at
protected override void CopyBytesImpl(long value, int bytes, byte[] buffer, int index)
{
for (int i=0; i < bytes; i++)
{
buffer[i+index] = unchecked((byte)(value&0xff));
value = value >> 8;
}
}
///
/// Returns a value built from the specified number of bytes from the given buffer,
/// starting at index.
///
/// The data in byte array format
/// The first index to use
/// The number of bytes to use
/// The value built from the given bytes
protected override long FromBytes(byte[] buffer, int startIndex, int bytesToConvert)
{
long ret = 0;
for (int i=0; i < bytesToConvert; i++)
{
ret = unchecked((ret << 8) | buffer[startIndex+bytesToConvert-1-i]);
}
return ret;
}
}
}