Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GetBytes,参数为sbyte类型_C# - Fatal编程技术网

C# GetBytes,参数为sbyte类型

C# GetBytes,参数为sbyte类型,c#,C#,为什么GetBytes返回两个元素的数组而不是一个元素的数组,尽管sbyte的存储只需要1个字节 byte[] byteArray = BitConverter.GetBytes((sbyte)127) GetBytes没有接受sbyte,因此您的sbyte被隐式转换为short,您调用GetBytes(short),它返回两个字节 您只需通过转换将sbyte转换为byte sbyte s = 127; byte[] byteArray = new[] { (byte)s }; 在转换为字节

为什么GetBytes返回两个元素的数组而不是一个元素的数组,尽管sbyte的存储只需要1个字节

byte[] byteArray = BitConverter.GetBytes((sbyte)127)

GetBytes
没有接受
sbyte
,因此您的
sbyte
被隐式转换为
short
,您调用
GetBytes(short)
,它返回两个字节

您只需通过转换将
sbyte
转换为
byte

sbyte s = 127;
byte[] byteArray = new[] { (byte)s };

在转换为字节之前,从它转换为
short
的位来看,似乎是这样的。我不能告诉你它为什么这样做,或者如何让它转换成字节。