78 lines
2.3 KiB
C#
78 lines
2.3 KiB
C#
using System.Data;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
using System.Runtime.InteropServices;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Text;
|
|
using static SQLite.SQLite3;
|
|
|
|
namespace OrbisLib2.Common.Helpers
|
|
{
|
|
public static class Helper
|
|
{
|
|
/// <summary>
|
|
/// Convert an object to a byte array
|
|
/// </summary>
|
|
/// <param name="obj">The Object to convert.</param>
|
|
/// <returns></returns>
|
|
public static byte[] ObjectToByteArray(Object obj)
|
|
{
|
|
BinaryFormatter bf = new BinaryFormatter();
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
bf.Serialize(ms, obj);
|
|
return ms.ToArray();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert a byte array to an Object
|
|
/// </summary>
|
|
/// <param name="arrBytes">The array to convert.</param>
|
|
/// <returns></returns>
|
|
public static Object ByteArrayToObject(byte[] arrBytes)
|
|
{
|
|
using (var memStream = new MemoryStream())
|
|
{
|
|
var binForm = new BinaryFormatter();
|
|
memStream.Write(arrBytes, 0, arrBytes.Length);
|
|
memStream.Seek(0, SeekOrigin.Begin);
|
|
return binForm.Deserialize(memStream);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static byte[] StructToBytes(object str)
|
|
{
|
|
int size = Marshal.SizeOf(str);
|
|
byte[] arr = new byte[size];
|
|
|
|
IntPtr ptr = Marshal.AllocHGlobal(size);
|
|
Marshal.StructureToPtr(str, ptr, true);
|
|
Marshal.Copy(ptr, arr, 0, size);
|
|
Marshal.FreeHGlobal(ptr);
|
|
return arr;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="arr"></param>
|
|
/// <param name="str"></param>
|
|
public static void BytesToStruct<T>(byte[] arr, ref T str)
|
|
{
|
|
int size = Marshal.SizeOf(str);
|
|
IntPtr ptr = Marshal.AllocHGlobal(size);
|
|
|
|
Marshal.Copy(arr, 0, ptr, size);
|
|
|
|
str = (T)Marshal.PtrToStructure(ptr, str.GetType());
|
|
Marshal.FreeHGlobal(ptr);
|
|
}
|
|
}
|
|
}
|