DESKTOP-5RP3AKU\Jisol c85f350e0a 临时提交
2024-10-17 01:59:25 +08:00

41 lines
1.1 KiB
C#

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace JNGame.Runtime.Util
{
public class ToUtil
{
public static int Byte4ToInt(byte[] data)
{
return BitConverter.ToInt32(data, 0);
}
public static byte[] IntToByte4(int value)
{
byte[] result = new byte[4];
BitConverter.GetBytes(value).CopyTo(result, 0);
return result;
}
public static byte[] ObjectToBytes<T>(T data)
{
var formatter = new BinaryFormatter();
using var mStream = new MemoryStream();
formatter.Serialize(mStream, data);
mStream.Flush();
return mStream.GetBuffer();
}
public static T BytesToObject<T>(byte[] data)
{
var formatter = new BinaryFormatter();
using var mStream = new MemoryStream();
mStream.Write(data, 0, data.Length);
mStream.Flush();
mStream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(mStream);
}
}
}