mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 11:24:46 +00:00
42 lines
1.1 KiB
C#
42 lines
1.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net.Sockets;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
|
|
namespace Plugins.JNGame.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);
|
|
}
|
|
|
|
}
|
|
} |