You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

45 lines
1.4 KiB

using Newtonsoft.Json;
using System.Text;
namespace Znyc.Cloudcar.Admin.Commons.Helpers
{
/// <summary>
/// byte转换类
/// </summary>
public class ByteConvertHelper
{
/// <summary>
/// 将对象转换为byte数组
/// </summary>
/// <param name="obj">被转换对象</param>
/// <returns>转换后byte数组</returns>
public static byte[] Object2Bytes(object obj)
{
string json = JsonConvert.SerializeObject(obj);
byte[] serializedResult = Encoding.UTF8.GetBytes(json);
return serializedResult;
}
/// <summary>
/// 将byte数组转换成对象
/// </summary>
/// <param name="buff">被转换byte数组</param>
/// <returns>转换完成后的对象</returns>
public static object Bytes2Object(byte[] buff)
{
string json = Encoding.UTF8.GetString(buff);
return JsonConvert.DeserializeObject<object>(json);
}
/// <summary>
/// 将byte数组转换成对象
/// </summary>
/// <param name="buff">被转换byte数组</param>
/// <returns>转换完成后的对象</returns>
public static T Bytes2Object<T>(byte[] buff)
{
string json = Encoding.UTF8.GetString(buff);
return JsonConvert.DeserializeObject<T>(json);
}
}
}