你有几个选择。它们取决于您将如何使用数据。您可以将其打包成 Guid、使用字节数组、使用自定义 Struct,或者将其打包为 Base64 或 HEX 并将其填充到字符串中。
[StructLayout( LayoutKind.Explicit)]
public struct MyStruct
{
public MyStruct(byte[] buffer)
{
if (buffer.Length != 10)
throw new ArgumentOutOfRangeException();
High = BitConverter.ToUInt16(buffer, 0);
Low = BitConverter.ToUInt64(buffer, 2);
}
[FieldOffset(0)]
public ushort High; //2 bytes
[FieldOffset(2)]
public ulong Low; //8 bytes
public byte[] Bytes
{
get
{
return BitConverter.GetBytes(High)
.Concat(BitConverter.GetBytes(Low))
.ToArray();
}
}
public override string ToString()
{
return Convert.ToBase64String(Bytes);
}
public static MyStruct Parse(string toParse)
{
var bytes = Convert.FromBase64String(toParse);
return new MyStruct(bytes);
}
}