LittleEndianConverter.cs 4.9 KB

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