BigEndianConverter.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Utilities
  4. {
  5. public class BigEndianConverter
  6. {
  7. public static ushort ToUInt16(byte[] buffer, int offset)
  8. {
  9. return (ushort)((buffer[offset + 0] << 8) | (buffer[offset + 1] << 0));
  10. }
  11. public static short ToInt16(byte[] buffer, int offset)
  12. {
  13. return (short)ToUInt16(buffer, offset);
  14. }
  15. public static uint ToUInt32(byte[] buffer, int offset)
  16. {
  17. return (uint)((buffer[offset + 0] << 24) | (buffer[offset + 1] << 16)
  18. | (buffer[offset + 2] << 8) | (buffer[offset + 3] << 0));
  19. }
  20. public static int ToInt32(byte[] buffer, int offset)
  21. {
  22. return (int)ToUInt32(buffer, offset);
  23. }
  24. public static ulong ToUInt64(byte[] buffer, int offset)
  25. {
  26. return (((ulong)ToUInt32(buffer, offset + 0)) << 32) | ToUInt32(buffer, offset + 4);
  27. }
  28. public static long ToInt64(byte[] buffer, int offset)
  29. {
  30. return (long)ToUInt64(buffer, offset);
  31. }
  32. public static Guid ToGuid(byte[] buffer, int offset)
  33. {
  34. return new Guid(
  35. ToUInt32(buffer, offset + 0),
  36. ToUInt16(buffer, offset + 4),
  37. ToUInt16(buffer, offset + 6),
  38. buffer[offset + 8],
  39. buffer[offset + 9],
  40. buffer[offset + 10],
  41. buffer[offset + 11],
  42. buffer[offset + 12],
  43. buffer[offset + 13],
  44. buffer[offset + 14],
  45. buffer[offset + 15]);
  46. }
  47. public static byte[] GetBytes(ushort value)
  48. {
  49. byte[] result = new byte[2];
  50. result[0] = (byte)((value >> 8) & 0xFF);
  51. result[1] = (byte)((value >> 0) & 0xFF);
  52. return result;
  53. }
  54. public static byte[] GetBytes(short value)
  55. {
  56. return GetBytes((ushort)value);
  57. }
  58. public static byte[] GetBytes(uint value)
  59. {
  60. byte[] result = new byte[4];
  61. result[0] = (byte)((value >> 24) & 0xFF);
  62. result[1] = (byte)((value >> 16) & 0xFF);
  63. result[2] = (byte)((value >> 8) & 0xFF);
  64. result[3] = (byte)((value >> 0) & 0xFF);
  65. return result;
  66. }
  67. public static byte[] GetBytes(int value)
  68. {
  69. return GetBytes((uint)value);
  70. }
  71. public static byte[] GetBytes(ulong value)
  72. {
  73. byte[] result = new byte[8];
  74. Array.Copy(GetBytes((uint)(value >> 32)), 0, result, 0, 4);
  75. Array.Copy(GetBytes((uint)(value & 0xFFFFFFFF)), 0, result, 4, 4);
  76. return result;
  77. }
  78. public static byte[] GetBytes(long value)
  79. {
  80. return GetBytes((ulong)value);
  81. }
  82. public static byte[] GetBytes(Guid value)
  83. {
  84. byte[] result = value.ToByteArray();
  85. if (BitConverter.IsLittleEndian)
  86. {
  87. // reverse first 4 bytes
  88. byte temp = result[0];
  89. result[0] = result[3];
  90. result[3] = temp;
  91. temp = result[1];
  92. result[1] = result[2];
  93. result[2] = temp;
  94. // reverse next 2 bytes
  95. temp = result[4];
  96. result[4] = result[5];
  97. result[5] = temp;
  98. // reverse next 2 bytes
  99. temp = result[6];
  100. result[6] = result[7];
  101. result[7] = temp;
  102. }
  103. return result;
  104. }
  105. }
  106. }