亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

c#,關于BigEndian和LittleEndian,以及轉換類

2019-11-14 13:40:54
字體:
來源:轉載
供稿:網友

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, 1);            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, 2)));        }        /// <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, 2)));        }        /// <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, 4)));        }        /// <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, 8);        }        /// <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, 2)));        }        /// <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, 4)));        }        /// <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, 8)));        }        /// <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 < 0 || 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[4];            for (int i=0; i < 4; i++)            {                parts[i] = ToInt32(value, startIndex+i*4);            }            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[16];            int[] parts = decimal.GetBits(value);            for (int i=0; i < 4; i++)            {                CopyBytesImpl(parts[i], 4, bytes, i*4);            }            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=0; i < 4; i++)            {                CopyBytesImpl(parts[i], 4, buffer, i*4+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, 0);            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, 2);        }        /// <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), 8);        }        /// <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, 2);        }        /// <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, 4);        }        /// <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, 8);        }        /// <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), 4);        }        /// <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, 2);        }        /// <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, 4);        }        /// <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), 8);        }        #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 ? 1 : 0, 1, 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, 2, 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), 8, 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, 2, 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, 4, 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, 8, 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), 4, 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, 2, 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, 4, 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), 8, 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(0)]            int i;            /// <summary>            /// Single version of the value.            /// </summary>            [FieldOffset(0)]            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 = 0; // 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 = 0; // 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-1;            for (int i=0; i < bytes; i++)            {                buffer[endOffset-i] = unchecked((byte)(value&0xff));                value = value >> 8;            }        }        /// <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 = 0;            for (int i=0; i < bytesToConvert; i++)            {                ret = unchecked((ret << 8) | 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=0; i < bytes; i++)            {                buffer[i+index] = unchecked((byte)(value&0xff));                value = value >> 8;            }        }        /// <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 = 0;            for (int i=0; i < bytesToConvert; i++)            {                ret = unchecked((ret << 8) | buffer[startIndex+bytesToConvert-1-i]);            }            return ret;        }    }}
LittleEndianBitConverter

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
午夜精品久久久久久久久久久久| 色偷偷88888欧美精品久久久| 亚洲桃花岛网站| 国产精品美女免费| 国产精品久久久久久超碰| 奇门遁甲1982国语版免费观看高清| 成年无码av片在线| 亚洲a一级视频| 日韩精品极品视频免费观看| 国产黑人绿帽在线第一区| 午夜精品理论片| 亚洲人成电影在线| 美日韩丰满少妇在线观看| 亚洲欧美制服中文字幕| 亚洲欧美在线免费观看| 亚洲国模精品私拍| 国产精品免费网站| 亚洲人成啪啪网站| 亚洲美女av网站| 日韩av一区二区在线观看| 欧美野外wwwxxx| 亚洲欧美日韩爽爽影院| 日韩中文字幕免费视频| 日韩欧美国产骚| 欧美激情极品视频| 久久久久亚洲精品| 色777狠狠综合秋免鲁丝| 91热精品视频| 日韩欧美精品免费在线| 日韩美女在线看| 亚洲国产精品视频在线观看| 日韩在线视频免费观看高清中文| 日韩精品一区二区视频| 欧美精品videos另类日本| www日韩欧美| 欧美大尺度电影在线观看| 日韩经典中文字幕| 欧美精品电影免费在线观看| 亚洲精品一区中文字幕乱码| 久热在线中文字幕色999舞| 国产一区二区黑人欧美xxxx| 在线播放亚洲激情| 欧美肥老妇视频| 久久久久久久999| 色综合影院在线| 日韩二区三区在线| 国产精品伦子伦免费视频| 国产欧美日韩91| 日本精品视频网站| 亚洲欧美中文日韩在线v日本| 高清亚洲成在人网站天堂| 国产精品免费一区| 亚洲国产精彩中文乱码av| 日韩精品在线视频| 午夜精品福利在线观看| 亚洲91精品在线| 国产精品日韩在线观看| 国模极品一区二区三区| 国产亚洲欧美日韩精品| 美女黄色丝袜一区| 国产综合香蕉五月婷在线| 国产精品香蕉av| 欧美一级电影免费在线观看| 亚洲视频在线看| 亚洲电影免费观看高清完整版| 91亚洲精品一区二区| 国产成人在线播放| 欧美电影在线观看网站| 日韩av网站在线| 亚洲国产精品久久久久秋霞不卡| 亚洲精品国精品久久99热一| 国产精品99久久久久久久久| 国产精品网站入口| 久久久久久香蕉网| 久久久久久久久久久免费精品| 精品毛片三在线观看| 2019亚洲日韩新视频| 欧美肥老太性生活视频| 国产精品免费久久久| 成人在线播放av| 国产婷婷成人久久av免费高清| 亚洲精品v天堂中文字幕| 日韩精品在线播放| 欧美日本啪啪无遮挡网站| 日本19禁啪啪免费观看www| 91免费高清视频| 亚洲自拍中文字幕| 国产成人精品在线视频| 97视频免费在线看| 欧美精品九九久久| 精品美女永久免费视频| 久久91亚洲精品中文字幕奶水| 91精品国产高清自在线| 在线观看欧美日韩国产| 国产精品美女网站| 色哟哟网站入口亚洲精品| 在线亚洲国产精品网| 92看片淫黄大片欧美看国产片| 日韩中文字幕免费视频| 国产精品揄拍一区二区| 色综合天天综合网国产成人网| 欧美大片网站在线观看| 亚洲国模精品一区| 国产免费一区二区三区在线观看| 亚洲精品999| 国产精品成av人在线视午夜片| 亚洲第一精品久久忘忧草社区| 综合网中文字幕| 在线视频免费一区二区| 美女视频黄免费的亚洲男人天堂| 亚洲福利视频网| 在线日韩第一页| 亚洲精品成人免费| 亚洲国产精品成人va在线观看| 亚洲丁香久久久| 91青草视频久久| 精品国产电影一区| 亚洲国产精品成人av| xvideos成人免费中文版| 亚洲成av人乱码色午夜| 亚洲国产中文字幕在线观看| 日韩欧美亚洲国产一区| 97久久超碰福利国产精品…| 亚洲97在线观看| 日韩av不卡在线| 国产精品久久一区主播| 久久香蕉国产线看观看av| 欧美成人手机在线| 国产一区二区三区精品久久久| 欧美孕妇毛茸茸xxxx| 91精品国产综合久久香蕉的用户体验| 国产欧美欧洲在线观看| 亚洲女人天堂成人av在线| 精品国产一区二区三区久久久狼| 欧美性猛交xxxx免费看| 91色在线视频| 亚洲国产精品高清久久久| 91在线看www| 在线日韩精品视频| 色哟哟入口国产精品| 91精品久久久久久久久中文字幕| 欧洲s码亚洲m码精品一区| 日韩免费视频在线观看| 成年人精品视频| 成人美女av在线直播| 日本久久久久久久| 久久久久免费视频| 日韩视频在线免费观看| 精品中文视频在线| 亚洲有声小说3d| 岛国av一区二区三区| 精品久久香蕉国产线看观看gif| 国产97色在线|日韩| 国产欧美精品va在线观看| 国产亚洲美女久久| 欧美激情奇米色| 中文字幕精品一区二区精品| 欧美尺度大的性做爰视频| 国产精品亚洲欧美导航| 日韩亚洲在线观看| 性欧美长视频免费观看不卡| 亚洲美女激情视频| 精品爽片免费看久久| 欧美激情在线观看视频|