Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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# 从32位到8位的字节转换_C# - Fatal编程技术网

C# 从32位到8位的字节转换

C# 从32位到8位的字节转换,c#,C#,您好,我想发送这个十六进制值,但我收到错误。。我发送字节值,**我的错误。。常量值不能转换为字节** class constant(){ public const byte MESSAGE_START = 0x1020; // command hex } public override IEnumerable<byte> ToBytes() { yield return constant.MESSAGE_START ; } 正如我在评论中所

您好,我想发送这个十六进制值,但我收到错误。。我发送字节值,**我的错误。。常量值不能转换为字节**

class constant(){
public const byte MESSAGE_START = 0x1020; // command hex
}


public override IEnumerable<byte> ToBytes()
        {
      yield return constant.MESSAGE_START ;

    }

正如我在评论中所说,值
MESSAGE\u START
短的
而不是
字节
。试试这个

class constant() {
  public const short MESSAGE_START = 0x1020; // command hex
}

public override IEnumerable<byte> ToBytes()
{
  yield return (byte) (constant.MESSAGE_START >> 8);
  yield return (byte) (constant.MESSAGE_START & 0xff);
}
class常量(){
public const短消息\u START=0x1020;//命令十六进制
}
公共重写IEnumerable ToBytes()
{
收益率返回(字节)(常量.MESSAGE_START>>8);
收益返回(字节)(常量.MESSAGE\u START&0xff);
}
上面的代码假定值的字节表示形式为in(最高有效字节优先)。对于LSBdo

public override IEnumerable<byte> ToBytes()
{
  yield return (byte) (constant.MESSAGE_START & 0xFF);
  yield return (byte) (constant.MESSAGE_START >> 8);
}
public覆盖IEnumerable ToBytes()
{
收益返回(字节)(常量.MESSAGE\u START&0xFF);
收益率返回(字节)(常量.MESSAGE_START>>8);
}

这是无效的C<代码>收益返回只能在返回IEnumerable的方法中使用<代码>消息\u START是一个
而不是
字节
。您好,对不起,请现在检查。byte[]results=BitConverter.GetBytes(1234567890);好的,既然我给他们传递了一个字节,那么我以后应该把它转换成字节吗?什么
ToBytes
正在播放。您好,请检查我的代码,因为我面临一个新问题。我已经回答了您的问题。请投票并接受它。创建一个新问题,然后获得新答案。如何将整个十六进制值0x1020传递到一个字节中,因为上面的代码将其拆分为10和20
public override IEnumerable<byte> ToBytes()
{
  yield return (byte) (constant.MESSAGE_START & 0xFF);
  yield return (byte) (constant.MESSAGE_START >> 8);
}