LittleEndianConverter.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Utilities
  4. {
  5. public class LittleEndianConverter
  6. {
  7. public static ushort ToUInt16(byte[] buffer, int offset)
  8. {
  9. return (ushort)((buffer[offset + 1] << 8) | (buffer[offset + 0] << 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 + 3] << 24) | (buffer[offset + 2] << 16)
  18. | (buffer[offset + 1] << 8) | (buffer[offset + 0] << 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 + 4)) << 32) | ToUInt32(buffer, offset + 0);
  27. }
  28. public static long ToInt64(byte[] buffer, int offset)
  29. {
  30. return (long)ToUInt64(buffer, offset);
  31. }
  32. public static float ToFloat32(byte[] buffer, int offset)
  33. {
  34. byte[] bytes = new byte[4];
  35. Array.Copy(buffer, offset, bytes, 0, 4);
  36. if (!BitConverter.IsLittleEndian)
  37. {
  38. // reverse the order of 'bytes'
  39. for (int index = 0; index < 2; index++)
  40. {
  41. byte temp = bytes[index];
  42. bytes[index] = bytes[3 - index];
  43. bytes[3 - index] = temp;
  44. }
  45. }
  46. return BitConverter.ToSingle(bytes, 0);
  47. }
  48. public static double ToFloat64(byte[] buffer, int offset)
  49. {
  50. byte[] bytes = new byte[8];
  51. Array.Copy(buffer, offset, bytes, 0, 8);
  52. if (!BitConverter.IsLittleEndian)
  53. {
  54. // reverse the order of 'bytes'
  55. for(int index = 0; index < 4; index++)
  56. {
  57. byte temp = bytes[index];
  58. bytes[index] = bytes[7 - index];
  59. bytes[7 - index] = temp;
  60. }
  61. }
  62. return BitConverter.ToDouble(bytes, 0);
  63. }
  64. public static Guid ToGuid(byte[] buffer, int offset)
  65. {
  66. return new Guid(
  67. ToUInt32(buffer, offset + 0),
  68. ToUInt16(buffer, offset + 4),
  69. ToUInt16(buffer, offset + 6),
  70. buffer[offset + 8],
  71. buffer[offset + 9],
  72. buffer[offset + 10],
  73. buffer[offset + 11],
  74. buffer[offset + 12],
  75. buffer[offset + 13],
  76. buffer[offset + 14],
  77. buffer[offset + 15]);
  78. }
  79. public static byte[] GetBytes(ushort value)
  80. {
  81. byte[] result = new byte[2];
  82. result[0] = (byte)((value >> 0) & 0xFF);
  83. result[1] = (byte)((value >> 8) & 0xFF);
  84. return result;
  85. }
  86. public static byte[] GetBytes(short value)
  87. {
  88. return GetBytes((ushort)value);
  89. }
  90. public static byte[] GetBytes(uint value)
  91. {
  92. byte[] result = new byte[4];
  93. result[0] = (byte)((value >> 0) & 0xFF);
  94. result[1] = (byte)((value >> 8) & 0xFF);
  95. result[2] = (byte)((value >> 16) & 0xFF);
  96. result[3] = (byte)((value >> 24) & 0xFF);
  97. return result;
  98. }
  99. public static byte[] GetBytes(int value)
  100. {
  101. return GetBytes((uint)value);
  102. }
  103. public static byte[] GetBytes(ulong value)
  104. {
  105. byte[] result = new byte[8];
  106. Array.Copy(GetBytes((uint)(value & 0xFFFFFFFF)), 0, result, 0, 4);
  107. Array.Copy(GetBytes((uint)(value >> 32)), 0, result, 4, 4);
  108. return result;
  109. }
  110. public static byte[] GetBytes(long value)
  111. {
  112. return GetBytes((ulong)value);
  113. }
  114. public static byte[] GetBytes(Guid value)
  115. {
  116. byte[] result = value.ToByteArray();
  117. if (!BitConverter.IsLittleEndian)
  118. {
  119. // reverse first 4 bytes
  120. byte temp = result[0];
  121. result[0] = result[3];
  122. result[3] = temp;
  123. temp = result[1];
  124. result[1] = result[2];
  125. result[2] = temp;
  126. // reverse next 2 bytes
  127. temp = result[4];
  128. result[4] = result[5];
  129. result[5] = temp;
  130. // reverse next 2 bytes
  131. temp = result[6];
  132. result[6] = result[7];
  133. result[7] = temp;
  134. }
  135. return result;
  136. }
  137. }
  138. }