0

I have the following function in C# where I try to extract different fields of my protocol header from a stream of bytes. I am getting a red-carpet under

m_SequnceNumber = inBytes[5] & (byte)0x7f;

It says cannot implictly convert in to byte.

My sequence number is only 7 bits. Eighth bit is used as a flag. What is wrong with the below code ?

public Header(byte[] inBytes)
{
    m_syncBytes = BitConverter.ToUInt16(inBytes,0);
    m_DestAddress = BitConverter.ToUInt16(inBytes, 2);
    m_SourceAddress = BitConverter.ToUInt16(inBytes, 3);
    m_Proto = inBytes[4];
    m_SequnceNumber = inBytes[5] & (byte)0x7f;

    m_Length = inBytes[7];
    m_HdrCrc = inBytes[8];
}
liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • 1
    I get no error on that line, *depending on what the type of m_SequenceNumber is* – BradleyDotNET Mar 06 '17 at 22:48
  • 2
    Try: `m_SequenceNumber = (byte)(inBytes[5] & 0x7f);` – itsme86 Mar 06 '17 at 22:49
  • 1
    This is like http://stackoverflow.com/questions/941584 but for the `&` operator instead. – Jon Skeet Mar 06 '17 at 22:50
  • @Jonskeet - slightly different . that answer is about arithmetic operators on byte (lack of). That makes sense, in general bytes are not used for arithmetic. A lack of logical operators on bytes seems more odd – pm100 Mar 06 '17 at 22:55
  • @pm100: Well, it's more that there are operators where `byte` operands are promoted to `int`, and so the result is `int` too... I agree it's slightly less sound for `byte`, but I'm saying the general gist is the same. – Jon Skeet Mar 06 '17 at 22:59

0 Answers0