c#,关于Big Endian 和 Little Endian,以及转换类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#,关于Big Endian 和 Little Endian,以及转换类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含31762字,纯文字阅读大概需要46分钟。
内容图文
![c#,关于Big Endian 和 Little Endian,以及转换类](/upload/InfoBanner/zyjiaocheng/1232/f5bc97bbf7214b4dacb53577d6c69e22.jpg)
Big Endian:最高字节在地址最低位,最低字节在地址最高位,依次排列。
Little Endian:最低字节在最低位,最高字节在最高位,反序排列。
当在本地主机上,无需注意机器用的是Big Endian还是Little Endian。但是网络上都是用的是Big Endian,需要进行一个转换,但是c#提供的BitConverter默认使用的Little Endian,在需与网络通信时,反的字节序大有不便,特此提供一个可选Big Endian和Little Endian类方便转换。
为什么存在Little Endian?因为对于机器来说,Little Endian更有利于机器的运算。内存地址由低位到高位,在两个数相加运算,直接在高位添加进位数,不必移动内存地址
第一部分
![技术分享](/upload/getfiles/default/2022/11/10/20221110112613627.jpg)
![技术分享](/upload/getfiles/default/2022/11/10/20221110112613705.jpg)
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>publicabstractbool IsLittleEndian(); ///<summary>/// Indicates the byte order ("endianess") in which data is converted using this class. ///</summary>publicabstract 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>publicstatic 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>publicstatic 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>publiclong 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>publicdouble 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>publicint SingleToInt32Bits(float value) { returnnew 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>publicfloat Int32BitsToSingle (int value) { returnnew 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>publicbool 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>publicchar ToChar (byte[] value, int startIndex) { returnunchecked((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>publicdouble 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>publicfloat 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>publicshort ToInt16 (byte[] value, int startIndex) { returnunchecked((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>publicint ToInt32 (byte[] value, int startIndex) { returnunchecked((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>publiclong 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>publicushort ToUInt16 (byte[] value, int startIndex) { returnunchecked((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>publicuint ToUInt32 (byte[] value, int startIndex) { returnunchecked((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>publiculong ToUInt64 (byte[] value, int startIndex) { returnunchecked((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>staticvoid CheckByteArgument(byte[] value, int startIndex, int bytesRequired) { if (value==null) { thrownew ArgumentNullException("value"); } if (startIndex < 0 || startIndex > value.Length-bytesRequired) { thrownew 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>protectedabstractlong 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>publicstaticstring 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>publicstaticstring 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>publicstaticstring 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>publicdecimal 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 = newint[4]; for (int i=0; i < 4; i++) { parts[i] = ToInt32(value, startIndex+i*4); } returnnew 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>publicbyte[] GetBytes(decimal value) { byte[] bytes = newbyte[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>publicvoid 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 = newbyte[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>publicbyte[] 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>publicbyte[] 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>publicbyte[] 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>publicbyte[] 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>publicbyte[] 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>publicbyte[] 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>publicbyte[] 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>publicbyte[] 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>publicbyte[] 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>publicbyte[] 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) { thrownew ArgumentNullException("buffer", "Byte array must not be null"); } if (buffer.Length < index+bytes) { thrownew 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>protectedabstractvoid 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>publicvoid 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>publicvoid 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>publicvoid 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>publicvoid 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>publicvoid 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>publicvoid 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>publicvoid 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>publicvoid 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>publicvoid 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>publicvoid 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 happythis.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 happythis.f = f; } ///<summary>/// Returns the value of the instance as an integer. ///</summary>internalint AsInt32 { get { return i; } } ///<summary>/// Returns the value of the instance as a floating point number. ///</summary>internalfloat AsSingle { get { return f; } } } #endregion } }
第二部分
![技术分享](/upload/getfiles/default/2022/11/10/20221110112613627.jpg)
![技术分享](/upload/getfiles/default/2022/11/10/20221110112613705.jpg)
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>protectedoverridevoid 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>protectedoverridelong 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; } } }
第三部分
![技术分享](/upload/getfiles/default/2022/11/10/20221110112613627.jpg)
![技术分享](/upload/getfiles/default/2022/11/10/20221110112613705.jpg)
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 } }
第四部分
![技术分享](/upload/getfiles/default/2022/11/10/20221110112613627.jpg)
![技术分享](/upload/getfiles/default/2022/11/10/20221110112613705.jpg)
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>protectedoverridevoid 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>protectedoverridelong 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; } } }
原文:http://www.cnblogs.com/mokeyish/p/5042812.html
内容总结
以上是互联网集市为您收集整理的c#,关于Big Endian 和 Little Endian,以及转换类全部内容,希望文章能够帮你解决c#,关于Big Endian 和 Little Endian,以及转换类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。