erlang自定义二进制协议
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

65 行
2.2 KiB

  1. 
  2. namespace MiscUtil.Conversion
  3. {
  4. /// <summary>
  5. /// Implementation of EndianBitConverter which converts to/from little-endian
  6. /// byte arrays.
  7. /// </summary>
  8. public sealed class LittleEndianBitConverter : EndianBitConverter
  9. {
  10. /// <summary>
  11. /// Indicates the byte order ("endianess") in which data is converted using this class.
  12. /// </summary>
  13. /// <remarks>
  14. /// Different computer architectures store data using different byte orders. "Big-endian"
  15. /// means the most significant byte is on the left end of a word. "Little-endian" means the
  16. /// most significant byte is on the right end of a word.
  17. /// </remarks>
  18. /// <returns>true if this converter is little-endian, false otherwise.</returns>
  19. public sealed override bool IsLittleEndian()
  20. {
  21. return true;
  22. }
  23. /// <summary>
  24. /// Indicates the byte order ("endianess") in which data is converted using this class.
  25. /// </summary>
  26. public sealed override Endianness Endianness
  27. {
  28. get { return Endianness.LittleEndian; }
  29. }
  30. /// <summary>
  31. /// Copies the specified number of bytes from value to buffer, starting at index.
  32. /// </summary>
  33. /// <param name="value">The value to copy</param>
  34. /// <param name="bytes">The number of bytes to copy</param>
  35. /// <param name="buffer">The buffer to copy the bytes into</param>
  36. /// <param name="index">The index to start at</param>
  37. protected override void CopyBytesImpl(long value, int bytes, byte[] buffer, int index)
  38. {
  39. for (int i=0; i < bytes; i++)
  40. {
  41. buffer[i+index] = unchecked((byte)(value&0xff));
  42. value = value >> 8;
  43. }
  44. }
  45. /// <summary>
  46. /// Returns a value built from the specified number of bytes from the given buffer,
  47. /// starting at index.
  48. /// </summary>
  49. /// <param name="buffer">The data in byte array format</param>
  50. /// <param name="startIndex">The first index to use</param>
  51. /// <param name="bytesToConvert">The number of bytes to use</param>
  52. /// <returns>The value built from the given bytes</returns>
  53. protected override long FromBytes(byte[] buffer, int startIndex, int bytesToConvert)
  54. {
  55. long ret = 0;
  56. for (int i=0; i < bytesToConvert; i++)
  57. {
  58. ret = unchecked((ret << 8) | buffer[startIndex+bytesToConvert-1-i]);
  59. }
  60. return ret;
  61. }
  62. }
  63. }