c#,关于Big Endian 和 Little Endian,以及转换类
Big Endian:最高字节在地址最低位,最低字节在地址最高位,依次排列。
Little Endian:最低字节在最低位,最高字节在最高位,反序排列。
当在本地主机上,无需注意机器用的是Big Endian还是Little Endian。但是网络上都是用的是Big Endian,需要进行一个转换,但是c#提供的BitConverter默认使用的Little Endian,在需与网络通信时,反的字节序大有不便,特此提供一个可选Big Endian和Little Endian类方便转换。
为什么存在Little Endian?因为对于机器来说,Little Endian更有利于机器的运算。内存地址由低位到高位,在两个数相加运算,直接在高位添加进位数,不必移动内存地址
第一部分
using System;
using System.Runtime.InteropServices; namespace MiscUtil.Conversion
{
/// <summary>
/// Equivalent of System.BitConverter, but with either endianness.
/// </summary>
public abstract class EndianBitConverter
{
#region Endianness of this converter
/// <summary>
/// Indicates the byte order ("endianess") in which data is converted using this class.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <returns>true if this converter is little-endian, false otherwise.</returns>
public abstract bool IsLittleEndian(); /// <summary>
/// Indicates the byte order ("endianess") in which data is converted using this class.
/// </summary>
public abstract Endianness Endianness { get; }
#endregion #region Factory properties
static LittleEndianBitConverter little = new LittleEndianBitConverter();
/// <summary>
/// Returns a little-endian bit converter instance. The same instance is
/// always returned.
/// </summary>
public static LittleEndianBitConverter Little
{
get { return little; }
} static BigEndianBitConverter big = new BigEndianBitConverter();
/// <summary>
/// Returns a big-endian bit converter instance. The same instance is
/// always returned.
/// </summary>
public static BigEndianBitConverter Big
{
get { return big; }
}
#endregion #region Double/primitive conversions
/// <summary>
/// Converts the specified double-precision floating point number to a
/// 64-bit signed integer. Note: the endianness of this converter does not
/// affect the returned value.
/// </summary>
/// <param name="value">The number to convert. </param>
/// <returns>A 64-bit signed integer whose value is equivalent to value.</returns>
public long DoubleToInt64Bits(double value)
{
return BitConverter.DoubleToInt64Bits(value);
} /// <summary>
/// Converts the specified 64-bit signed integer to a double-precision
/// floating point number. Note: the endianness of this converter does not
/// affect the returned value.
/// </summary>
/// <param name="value">The number to convert. </param>
/// <returns>A double-precision floating point number whose value is equivalent to value.</returns>
public double Int64BitsToDouble (long value)
{
return BitConverter.Int64BitsToDouble(value);
} /// <summary>
/// Converts the specified single-precision floating point number to a
/// 32-bit signed integer. Note: the endianness of this converter does not
/// affect the returned value.
/// </summary>
/// <param name="value">The number to convert. </param>
/// <returns>A 32-bit signed integer whose value is equivalent to value.</returns>
public int SingleToInt32Bits(float value)
{
return new Int32SingleUnion(value).AsInt32;
} /// <summary>
/// Converts the specified 32-bit signed integer to a single-precision floating point
/// number. Note: the endianness of this converter does not
/// affect the returned value.
/// </summary>
/// <param name="value">The number to convert. </param>
/// <returns>A single-precision floating point number whose value is equivalent to value.</returns>
public float Int32BitsToSingle (int value)
{
return new Int32SingleUnion(value).AsSingle;
}
#endregion #region To(PrimitiveType) conversions
/// <summary>
/// Returns a Boolean value converted from one byte at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>true if the byte at startIndex in value is nonzero; otherwise, false.</returns>
public bool ToBoolean (byte[] value, int startIndex)
{
CheckByteArgument(value, startIndex, );
return BitConverter.ToBoolean(value, startIndex);
} /// <summary>
/// Returns a Unicode character converted from two bytes at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A character formed by two bytes beginning at startIndex.</returns>
public char ToChar (byte[] value, int startIndex)
{
return unchecked((char) (CheckedFromBytes(value, startIndex, )));
} /// <summary>
/// Returns a double-precision floating point number converted from eight bytes
/// at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A double precision floating point number formed by eight bytes beginning at startIndex.</returns>
public double ToDouble (byte[] value, int startIndex)
{
return Int64BitsToDouble(ToInt64(value, startIndex));
} /// <summary>
/// Returns a single-precision floating point number converted from four bytes
/// at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A single precision floating point number formed by four bytes beginning at startIndex.</returns>
public float ToSingle (byte[] value, int startIndex)
{
return Int32BitsToSingle(ToInt32(value, startIndex));
} /// <summary>
/// Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A 16-bit signed integer formed by two bytes beginning at startIndex.</returns>
public short ToInt16 (byte[] value, int startIndex)
{
return unchecked((short) (CheckedFromBytes(value, startIndex, )));
} /// <summary>
/// Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A 32-bit signed integer formed by four bytes beginning at startIndex.</returns>
public int ToInt32 (byte[] value, int startIndex)
{
return unchecked((int) (CheckedFromBytes(value, startIndex, )));
} /// <summary>
/// Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A 64-bit signed integer formed by eight bytes beginning at startIndex.</returns>
public long ToInt64 (byte[] value, int startIndex)
{
return CheckedFromBytes(value, startIndex, );
} /// <summary>
/// Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A 16-bit unsigned integer formed by two bytes beginning at startIndex.</returns>
public ushort ToUInt16 (byte[] value, int startIndex)
{
return unchecked((ushort) (CheckedFromBytes(value, startIndex, )));
} /// <summary>
/// Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A 32-bit unsigned integer formed by four bytes beginning at startIndex.</returns>
public uint ToUInt32 (byte[] value, int startIndex)
{
return unchecked((uint) (CheckedFromBytes(value, startIndex, )));
} /// <summary>
/// Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A 64-bit unsigned integer formed by eight bytes beginning at startIndex.</returns>
public ulong ToUInt64 (byte[] value, int startIndex)
{
return unchecked((ulong) (CheckedFromBytes(value, startIndex, )));
} /// <summary>
/// Checks the given argument for validity.
/// </summary>
/// <param name="value">The byte array passed in</param>
/// <param name="startIndex">The start index passed in</param>
/// <param name="bytesRequired">The number of bytes required</param>
/// <exception cref="ArgumentNullException">value is a null reference</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// startIndex is less than zero or greater than the length of value minus bytesRequired.
/// </exception>
static void CheckByteArgument(byte[] value, int startIndex, int bytesRequired)
{
if (value==null)
{
throw new ArgumentNullException("value");
}
if (startIndex < || startIndex > value.Length-bytesRequired)
{
throw new ArgumentOutOfRangeException("startIndex");
}
} /// <summary>
/// Checks the arguments for validity before calling FromBytes
/// (which can therefore assume the arguments are valid).
/// </summary>
/// <param name="value">The bytes to convert after checking</param>
/// <param name="startIndex">The index of the first byte to convert</param>
/// <param name="bytesToConvert">The number of bytes to convert</param>
/// <returns></returns>
long CheckedFromBytes(byte[] value, int startIndex, int bytesToConvert)
{
CheckByteArgument(value, startIndex, bytesToConvert);
return FromBytes(value, startIndex, bytesToConvert);
} /// <summary>
/// Convert the given number of bytes from the given array, from the given start
/// position, into a long, using the bytes as the least significant part of the long.
/// By the time this is called, the arguments have been checked for validity.
/// </summary>
/// <param name="value">The bytes to convert</param>
/// <param name="startIndex">The index of the first byte to convert</param>
/// <param name="bytesToConvert">The number of bytes to use in the conversion</param>
/// <returns>The converted number</returns>
protected abstract long FromBytes(byte[] value, int startIndex, int bytesToConvert);
#endregion #region ToString conversions
/// <summary>
/// Returns a String converted from the elements of a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <remarks>All the elements of value are converted.</remarks>
/// <returns>
/// A String of hexadecimal pairs separated by hyphens, where each pair
/// represents the corresponding element in value; for example, "7F-2C-4A".
/// </returns>
public static string ToString(byte[] value)
{
return BitConverter.ToString(value);
} /// <summary>
/// Returns a String converted from the elements of a byte array starting at a specified array position.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <remarks>The elements from array position startIndex to the end of the array are converted.</remarks>
/// <returns>
/// A String of hexadecimal pairs separated by hyphens, where each pair
/// represents the corresponding element in value; for example, "7F-2C-4A".
/// </returns>
public static string ToString(byte[] value, int startIndex)
{
return BitConverter.ToString(value, startIndex);
} /// <summary>
/// Returns a String converted from a specified number of bytes at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <param name="length">The number of bytes to convert.</param>
/// <remarks>The length elements from array position startIndex are converted.</remarks>
/// <returns>
/// A String of hexadecimal pairs separated by hyphens, where each pair
/// represents the corresponding element in value; for example, "7F-2C-4A".
/// </returns>
public static string ToString(byte[] value, int startIndex, int length)
{
return BitConverter.ToString(value, startIndex, length);
}
#endregion #region Decimal conversions
/// <summary>
/// Returns a decimal value converted from sixteen bytes
/// at a specified position in a byte array.
/// </summary>
/// <param name="value">An array of bytes.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A decimal formed by sixteen bytes beginning at startIndex.</returns>
public decimal ToDecimal (byte[] value, int startIndex)
{
// HACK: This always assumes four parts, each in their own endianness,
// starting with the first part at the start of the byte array.
// On the other hand, there's no real format specified...
int[] parts = new int[];
for (int i=; i < ; i++)
{
parts[i] = ToInt32(value, startIndex+i*);
}
return new Decimal(parts);
} /// <summary>
/// Returns the specified decimal value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 16.</returns>
public byte[] GetBytes(decimal value)
{
byte[] bytes = new byte[];
int[] parts = decimal.GetBits(value);
for (int i=; i < ; i++)
{
CopyBytesImpl(parts[i], , bytes, i*);
}
return bytes;
} /// <summary>
/// Copies the specified decimal value into the specified byte array,
/// beginning at the specified index.
/// </summary>
/// <param name="value">A character to convert.</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
public void CopyBytes(decimal value, byte[] buffer, int index)
{
int[] parts = decimal.GetBits(value);
for (int i=; i < ; i++)
{
CopyBytesImpl(parts[i], , buffer, i*+index);
}
}
#endregion #region GetBytes conversions
/// <summary>
/// Returns an array with the given number of bytes formed
/// from the least significant bytes of the specified value.
/// This is used to implement the other GetBytes methods.
/// </summary>
/// <param name="value">The value to get bytes for</param>
/// <param name="bytes">The number of significant bytes to return</param>
byte[] GetBytes(long value, int bytes)
{
byte[] buffer = new byte[bytes];
CopyBytes(value, bytes, buffer, );
return buffer;
} /// <summary>
/// Returns the specified Boolean value as an array of bytes.
/// </summary>
/// <param name="value">A Boolean value.</param>
/// <returns>An array of bytes with length 1.</returns>
public byte[] GetBytes(bool value)
{
return BitConverter.GetBytes(value);
} /// <summary>
/// Returns the specified Unicode character value as an array of bytes.
/// </summary>
/// <param name="value">A character to convert.</param>
/// <returns>An array of bytes with length 2.</returns>
public byte[] GetBytes(char value)
{
return GetBytes(value, );
} /// <summary>
/// Returns the specified double-precision floating point value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
public byte[] GetBytes(double value)
{
return GetBytes(DoubleToInt64Bits(value), );
} /// <summary>
/// Returns the specified 16-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 2.</returns>
public byte[] GetBytes(short value)
{
return GetBytes(value, );
} /// <summary>
/// Returns the specified 32-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
public byte[] GetBytes(int value)
{
return GetBytes(value, );
} /// <summary>
/// Returns the specified 64-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
public byte[] GetBytes(long value)
{
return GetBytes(value, );
} /// <summary>
/// Returns the specified single-precision floating point value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
public byte[] GetBytes(float value)
{
return GetBytes(SingleToInt32Bits(value), );
} /// <summary>
/// Returns the specified 16-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 2.</returns>
public byte[] GetBytes(ushort value)
{
return GetBytes(value, );
} /// <summary>
/// Returns the specified 32-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
public byte[] GetBytes(uint value)
{
return GetBytes(value, );
} /// <summary>
/// Returns the specified 64-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
public byte[] GetBytes(ulong value)
{
return GetBytes(unchecked((long)value), );
} #endregion #region CopyBytes conversions
/// <summary>
/// Copies the given number of bytes from the least-specific
/// end of the specified value into the specified byte array, beginning
/// at the specified index.
/// This is used to implement the other CopyBytes methods.
/// </summary>
/// <param name="value">The value to copy bytes for</param>
/// <param name="bytes">The number of significant bytes to copy</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
void CopyBytes(long value, int bytes, byte[] buffer, int index)
{
if (buffer==null)
{
throw new ArgumentNullException("buffer", "Byte array must not be null");
}
if (buffer.Length < index+bytes)
{
throw new ArgumentOutOfRangeException("Buffer not big enough for value");
}
CopyBytesImpl(value, bytes, buffer, index);
} /// <summary>
/// Copies the given number of bytes from the least-specific
/// end of the specified value into the specified byte array, beginning
/// at the specified index.
/// This must be implemented in concrete derived classes, but the implementation
/// may assume that the value will fit into the buffer.
/// </summary>
/// <param name="value">The value to copy bytes for</param>
/// <param name="bytes">The number of significant bytes to copy</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
protected abstract void CopyBytesImpl(long value, int bytes, byte[] buffer, int index); /// <summary>
/// Copies the specified Boolean value into the specified byte array,
/// beginning at the specified index.
/// </summary>
/// <param name="value">A Boolean value.</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
public void CopyBytes(bool value, byte[] buffer, int index)
{
CopyBytes(value ? : , , buffer, index);
} /// <summary>
/// Copies the specified Unicode character value into the specified byte array,
/// beginning at the specified index.
/// </summary>
/// <param name="value">A character to convert.</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
public void CopyBytes(char value, byte[] buffer, int index)
{
CopyBytes(value, , buffer, index);
} /// <summary>
/// Copies the specified double-precision floating point value into the specified byte array,
/// beginning at the specified index.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
public void CopyBytes(double value, byte[] buffer, int index)
{
CopyBytes(DoubleToInt64Bits(value), , buffer, index);
} /// <summary>
/// Copies the specified 16-bit signed integer value into the specified byte array,
/// beginning at the specified index.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
public void CopyBytes(short value, byte[] buffer, int index)
{
CopyBytes(value, , buffer, index);
} /// <summary>
/// Copies the specified 32-bit signed integer value into the specified byte array,
/// beginning at the specified index.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
public void CopyBytes(int value, byte[] buffer, int index)
{
CopyBytes(value, , buffer, index);
} /// <summary>
/// Copies the specified 64-bit signed integer value into the specified byte array,
/// beginning at the specified index.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
public void CopyBytes(long value, byte[] buffer, int index)
{
CopyBytes(value, , buffer, index);
} /// <summary>
/// Copies the specified single-precision floating point value into the specified byte array,
/// beginning at the specified index.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
public void CopyBytes(float value, byte[] buffer, int index)
{
CopyBytes(SingleToInt32Bits(value), , buffer, index);
} /// <summary>
/// Copies the specified 16-bit unsigned integer value into the specified byte array,
/// beginning at the specified index.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
public void CopyBytes(ushort value, byte[] buffer, int index)
{
CopyBytes(value, , buffer, index);
} /// <summary>
/// Copies the specified 32-bit unsigned integer value into the specified byte array,
/// beginning at the specified index.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
public void CopyBytes(uint value, byte[] buffer, int index)
{
CopyBytes(value, , buffer, index);
} /// <summary>
/// Copies the specified 64-bit unsigned integer value into the specified byte array,
/// beginning at the specified index.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="buffer">The byte array to copy the bytes into</param>
/// <param name="index">The first index into the array to copy the bytes into</param>
public void CopyBytes(ulong value, byte[] buffer, int index)
{
CopyBytes(unchecked((long)value), , buffer, index);
} #endregion #region Private struct used for Single/Int32 conversions
/// <summary>
/// Union used solely for the equivalent of DoubleToInt64Bits and vice versa.
/// </summary>
[StructLayout(LayoutKind.Explicit)]
struct Int32SingleUnion
{
/// <summary>
/// Int32 version of the value.
/// </summary>
[FieldOffset()]
int i;
/// <summary>
/// Single version of the value.
/// </summary>
[FieldOffset()]
float f; /// <summary>
/// Creates an instance representing the given integer.
/// </summary>
/// <param name="i">The integer value of the new instance.</param>
internal Int32SingleUnion(int i)
{
this.f = ; // Just to keep the compiler happy
this.i = i;
} /// <summary>
/// Creates an instance representing the given floating point number.
/// </summary>
/// <param name="f">The floating point value of the new instance.</param>
internal Int32SingleUnion(float f)
{
this.i = ; // Just to keep the compiler happy
this.f = f;
} /// <summary>
/// Returns the value of the instance as an integer.
/// </summary>
internal int AsInt32
{
get { return i; }
} /// <summary>
/// Returns the value of the instance as a floating point number.
/// </summary>
internal float AsSingle
{
get { return f; }
}
}
#endregion
}
}
EndianBitConverter
第二部分
namespace MiscUtil.Conversion
{
/// <summary>
/// Implementation of EndianBitConverter which converts to/from big-endian
/// byte arrays.
/// </summary>
public sealed class BigEndianBitConverter : EndianBitConverter
{
/// <summary>
/// Indicates the byte order ("endianess") in which data is converted using this class.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <returns>true if this converter is little-endian, false otherwise.</returns>
public sealed override bool IsLittleEndian()
{
return false;
} /// <summary>
/// Indicates the byte order ("endianess") in which data is converted using this class.
/// </summary>
public sealed override Endianness Endianness
{
get { return Endianness.BigEndian; }
} /// <summary>
/// Copies the specified number of bytes from value to buffer, starting at index.
/// </summary>
/// <param name="value">The value to copy</param>
/// <param name="bytes">The number of bytes to copy</param>
/// <param name="buffer">The buffer to copy the bytes into</param>
/// <param name="index">The index to start at</param>
protected override void CopyBytesImpl(long value, int bytes, byte[] buffer, int index)
{
int endOffset = index+bytes-;
for (int i=; i < bytes; i++)
{
buffer[endOffset-i] = unchecked((byte)(value&0xff));
value = value >> ;
}
} /// <summary>
/// Returns a value built from the specified number of bytes from the given buffer,
/// starting at index.
/// </summary>
/// <param name="buffer">The data in byte array format</param>
/// <param name="startIndex">The first index to use</param>
/// <param name="bytesToConvert">The number of bytes to use</param>
/// <returns>The value built from the given bytes</returns>
protected override long FromBytes(byte[] buffer, int startIndex, int bytesToConvert)
{
long ret = ;
for (int i=; i < bytesToConvert; i++)
{
ret = unchecked((ret << ) | buffer[startIndex+i]);
}
return ret;
}
}
}
BigEndianBitConverter
第三部分
namespace MiscUtil.Conversion
{
/// <summary>
/// Endianness of a converter
/// </summary>
public enum Endianness
{
/// <summary>
/// Little endian - least significant byte first
/// </summary>
LittleEndian,
/// <summary>
/// Big endian - most significant byte first
/// </summary>
BigEndian
}
}
Endianness
第四部分
namespace MiscUtil.Conversion
{
/// <summary>
/// Implementation of EndianBitConverter which converts to/from little-endian
/// byte arrays.
/// </summary>
public sealed class LittleEndianBitConverter : EndianBitConverter
{
/// <summary>
/// Indicates the byte order ("endianess") in which data is converted using this class.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <returns>true if this converter is little-endian, false otherwise.</returns>
public sealed override bool IsLittleEndian()
{
return true;
} /// <summary>
/// Indicates the byte order ("endianess") in which data is converted using this class.
/// </summary>
public sealed override Endianness Endianness
{
get { return Endianness.LittleEndian; }
} /// <summary>
/// Copies the specified number of bytes from value to buffer, starting at index.
/// </summary>
/// <param name="value">The value to copy</param>
/// <param name="bytes">The number of bytes to copy</param>
/// <param name="buffer">The buffer to copy the bytes into</param>
/// <param name="index">The index to start at</param>
protected override void CopyBytesImpl(long value, int bytes, byte[] buffer, int index)
{
for (int i=; i < bytes; i++)
{
buffer[i+index] = unchecked((byte)(value&0xff));
value = value >> ;
}
} /// <summary>
/// Returns a value built from the specified number of bytes from the given buffer,
/// starting at index.
/// </summary>
/// <param name="buffer">The data in byte array format</param>
/// <param name="startIndex">The first index to use</param>
/// <param name="bytesToConvert">The number of bytes to use</param>
/// <returns>The value built from the given bytes</returns>
protected override long FromBytes(byte[] buffer, int startIndex, int bytesToConvert)
{
long ret = ;
for (int i=; i < bytesToConvert; i++)
{
ret = unchecked((ret << ) | buffer[startIndex+bytesToConvert--i]);
}
return ret;
}
}
}
LittleEndianBitConverter
c#,关于Big Endian 和 Little Endian,以及转换类的更多相关文章
- 字符编码笔记:ASCII,Unicode和UTF-8,附带 Little endian和Big endian的解释
作者: 阮一峰 日期: 2007年10月28日 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问题比我想象的复杂,从午饭后一直看到晚上9点,才算初步 ...
- Endian.BIG_ENDIAN和Endian.LITTLE_ENDIAN(http://smartblack.iteye.com/blog/1129097)
Endian.BIG_ENDIAN和Endian.LITTLE_ENDIAN 在ByteArray和Socket中,能看到一个属性endain. endian : String 更改或读取数据的字节顺 ...
- 大endian和little endian
大endian和little endian 一般Intel处理器或X86平台是小端 ,只是有点老了摩托罗拉的处理器将采用大端,掌握一下小端序. 小端序一般指低地址存低字节.高地址存高 ...
- Big Endian与Litter Endian
Big Endian是大端,Litter Endian是小端,意思很明了,但是很难记住谁是谁.每次涉及到这个概念的时候,我都会GOOGLE一下,浪费精力. 怎样才能永远记住他们呢?网上搜索了一下,有很 ...
- 数据在内存中的存储方式( Big Endian和Little Endian的区别 )(x86系列则采用little endian方式存储数据)
https://www.cnblogs.com/renyuan/archive/2013/05/26/3099766.html 1.故事的起源 “endian”这个词出自<格列佛游记>.小 ...
- 关于Big Endian 和 Little Endian
Big Endian 和 Little Endian 一.字节序 来自:http://ayazh.gjjblog.com/archives/1058846/ 谈到字节序的问题,必然牵涉到两大CPU派系 ...
- java代码中存在的Big Endian 和 Little Endian
Big Endian 和 Little Endian 详解 Java中的Big(Little)-endian问题的一种解决方法 主机序和网络序 很重要很重要 几种ip存放形式 Big-Endian和 ...
- 大端和小端(Big endian and Little endian)
一.大端和小端的问题 对于整型.长整型等数据类型,Big endian 认为第一个字节是最高位字节(按照从低地址到高地址的顺序存放数据的高位字节到低位字节):而 Little endian 则相反,它 ...
- 大端和小端(big endian little endian)
一.大端和小端的问题 对于整型.长整型等数据类型,Big endian 认为第一个字节是最高位字节(按照从低地址到高地址的顺序存放数据的高位字节到低位字节):而 Little endian 则相反,它 ...
随机推荐
- CSS中的rem
为什么会使用rem呢?主要还是浏览器和设备的大小不一. 这样就涉及到页面布局的不统一啦,先说说pc中的多栏布局吧,多栏布局有三种基本的实现方式:固定宽度.流动.弹性,下面我们就分别说说这三种布局吧. ...
- Sql Server来龙去脉系列之四 数据库和文件
在讨论数据库之前我们先要明白一个问题:什么是数据库? 数据库是若干对象的集合,这些对象用来控制和维护数据.一个经典的数据库实例仅仅包含少量的数据库,但用户一般也不会在一个实例上创建太多 ...
- 面向对象的JavaScript(一)命名空间
在小项目中对于JavaScript使用,只要写几个function就行了.但在大型项目中,尤其是在开发追求良好的用户体验的网站中,如SNS,就会用到大量的JavaScrpt,有时JavaScript的 ...
- Ext.NET 4.1 最新版本破解
Ext.NET 4.1 最新版本破解 今天在将Ext.NET 4.1版本的程序发布到公网时居然要license(localhost和127.0.0.1不收费),而且一年$4999,突然间觉得这是什么鬼 ...
- 用stimulsoft Reports报表工具制作简单报表的过程
这是在数据库sql server中People表的数据
- TCMalloc优化MySQL、Nginx内存管理
TCMalloc的全称为Thread-Caching Malloc,是谷歌开发的开源工具google-perftools中的一个成员. 与标准的glibc库的Malloc相比,TCMalloc库在内存 ...
- [小北De编程手记] : Lesson 01 - Selenium For C# 之 环境搭建
在我看来一个自动化测试平台的构建,是一种很好的了解开发语言,单元测试框架,自动化测试驱动,设计模式等等等的途径.因此,在下选择了自动化测试的这个话题来和大家分享一下本人关于软件开发和自动化测试的认识. ...
- Android应用与系统安全防御
来源:HTTP://WWW.CNBLOGS.COM/GOODHACKER/P/3864680.HTML ANDROID应用安全防御 Android应用的安全隐患包括三个方面:代码安全.数据安全和组件安 ...
- 错误:StrictMode $ AndroidBlockGuardPolicy.onNetwork
you have to insert 2 lines "StrictMode" on MainActivity Class, example's below: 在onCreate( ...
- iOS沙盒简单介绍
先简单介绍一下什么是沙盒:你可以简单理解成为一个目录,这个目录的改动不会对操作系统造成任何损失.(这里也有一点点介绍) 看看苹果的沙盒目录: 再附一张苹果官方的图 一个iOS app操作都是在自己的沙 ...