BigEndianConverter.cs 3.7 KB

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