ByteStruct.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.EndPoints.CytEndPoints
  8. {
  9. public class ByteStructConverter
  10. {
  11. public static T ToStruct<T>(byte[] by) where T : struct
  12. {
  13. int objectSize = Marshal.SizeOf(typeof(T));
  14. if (objectSize > by.Length) return default(T);
  15. // Allocate some unmanaged memory.
  16. IntPtr buffer = Marshal.AllocHGlobal(objectSize);
  17. // Copy the read byte array (byte[]) into the unmanaged memory block.
  18. Marshal.Copy(by, 0, buffer, objectSize);
  19. // Push the memory into a new struct of type (T).
  20. T returnStruct = (T)Marshal.PtrToStructure(buffer, typeof(T));
  21. // Free the unmanaged memory block.
  22. Marshal.FreeHGlobal(buffer);
  23. return returnStruct;
  24. }
  25. public static object ToStruct(byte[] buffer, Type t)
  26. {
  27. int objectSize = Marshal.SizeOf(t);
  28. if (objectSize > buffer.Length) return null;
  29. // Allocate some unmanaged memory.
  30. IntPtr buf = Marshal.AllocHGlobal(objectSize);
  31. // Copy the read byte array (byte[]) into the unmanaged memory block.
  32. Marshal.Copy(buffer, 0, buf, objectSize);
  33. // Push the memory into a new struct of type (T).
  34. object result = Marshal.PtrToStructure(buf, t);
  35. // Free the unmanaged memory block.
  36. Marshal.FreeHGlobal(buf);
  37. return result;
  38. }
  39. public static byte[] Struct2Bytes(object o)
  40. {
  41. // create a new byte buffer the size of your struct
  42. byte[] buffer = new byte[Marshal.SizeOf(o)];
  43. // pin the buffer so we can copy data into it w/o GC collecting it
  44. GCHandle bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
  45. // copy the struct data into the buffer
  46. Marshal.StructureToPtr(o, bufferHandle.AddrOfPinnedObject(), false);
  47. // free the GC handle
  48. bufferHandle.Free();
  49. return buffer;
  50. }
  51. }
  52. public class ByteReader
  53. {
  54. private byte[] buffer;
  55. private int idx;
  56. private int length;
  57. public int Length => length;
  58. public ByteReader(byte[] data, int len)
  59. {
  60. buffer = new byte[len];
  61. System.Buffer.BlockCopy(data, 0, buffer, 0, len);
  62. length = len;
  63. idx = 0;
  64. }
  65. public void Append(byte[] data, int len)
  66. {
  67. var buf = new byte[Length + len];
  68. System.Buffer.BlockCopy(buffer, 0, buf, 0, Length);
  69. System.Buffer.BlockCopy(data, 0, buf, Length, len);
  70. buffer = buf;
  71. length += len;
  72. }
  73. public bool Reset(int curIdx = 0)
  74. {
  75. if (curIdx < length)
  76. idx = curIdx;
  77. return idx == curIdx;
  78. }
  79. public bool ReadBytes(byte[] buf, int len)
  80. {
  81. if (idx + len <= length)
  82. {
  83. System.Buffer.BlockCopy(buffer, (int)idx, buf, 0, (int)len);
  84. idx += len;
  85. return true;
  86. }
  87. return false;
  88. }
  89. public bool ReadInt(out int data)
  90. {
  91. data = 0;
  92. if (idx + 4 <= length)
  93. {
  94. data = BitConverter.ToInt32(buffer, (int)idx);
  95. idx += 4;
  96. return true;
  97. }
  98. return false;
  99. }
  100. public bool ReadUInt16(out UInt16 data)
  101. {
  102. data = 0;
  103. if (idx + 2 <= length)
  104. {
  105. data = BitConverter.ToUInt16(buffer, (int)idx);
  106. idx += 2;
  107. return true;
  108. }
  109. return false;
  110. }
  111. public bool ReadInt64(out Int64 data)
  112. {
  113. data = 0;
  114. if (idx + 8 <= length)
  115. {
  116. data = BitConverter.ToInt64(buffer, (int)idx);
  117. idx += 8;
  118. return true;
  119. }
  120. return false;
  121. }
  122. }
  123. }