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);
}