C# 数据类型转化为byte数组

时间:2023-03-09 08:57:18
C# 数据类型转化为byte数组

short数据与byte数组互转

public byte[] ShortToByte(short value)
{
return BitConverter.GetBytes(value);
}
public short ByteToShort(byte[] arr)
{
return BitConverter.ToInt16(arr,);
}

string数据与byte数组互转

public byte[] StringToByte(string value)
{
return Encoding.UTF8.GetBytes(value);
//return Text.Encoding.Default.GetBytes (value);
}
public string ByteToString(byte[] arr)
{
return Encoding.UTF8.GetString(arr);
//return Text.Encoding.UTF8.GetString (arr, 0, arr.Length);
}

int数据与byte数组互转

public byte[] IntToByte(short value)
{
return BitConverter.GetBytes(value);
}
public int ByteToInt(byte[] arr)
{
return BitConverter.ToInt32(arr,);
}