为什么 GetBytes 返回两个元素的数组而不是一个元素的数组,尽管 sbyte 的存储只需要 1 个字节。
byte[] byteArray = BitConverter.GetBytes((sbyte)127)
最佳答案
GetBytes
没有 an overload它需要一个 sbyte
,所以你的 sbyte
被隐式转换为 short
并且你调用 GetBytes(short)
,返回两个字节。
你应该简单地将你的 sbyte
转换为一个带有 unchecked
的 byte
转换。
sbyte s = 127;
byte[] byteArray = new[] { (byte)s };
关于c# - 参数为 sbyte 类型的 GetBytes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23613440/