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

66 行
2.2 KiB

  1. 
  2. namespace MiscUtil.Conversion
  3. {
  4. /// <summary>
  5. /// Implementation of EndianBitConverter which converts to/from big-endian
  6. /// byte arrays.
  7. /// </summary>
  8. public sealed class BigEndianBitConverter : 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 false;
  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.BigEndian; }
  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. int endOffset = index+bytes-1;
  40. for (int i=0; i < bytes; i++)
  41. {
  42. buffer[endOffset-i] = unchecked((byte)(value&0xff));
  43. value = value >> 8;
  44. }
  45. }
  46. /// <summary>
  47. /// Returns a value built from the specified number of bytes from the given buffer,
  48. /// starting at index.
  49. /// </summary>
  50. /// <param name="buffer">The data in byte array format</param>
  51. /// <param name="startIndex">The first index to use</param>
  52. /// <param name="bytesToConvert">The number of bytes to use</param>
  53. /// <returns>The value built from the given bytes</returns>
  54. protected override long FromBytes(byte[] buffer, int startIndex, int bytesToConvert)
  55. {
  56. long ret = 0;
  57. for (int i=0; i < bytesToConvert; i++)
  58. {
  59. ret = unchecked((ret << 8) | buffer[startIndex+i]);
  60. }
  61. return ret;
  62. }
  63. }
  64. }