I have a byte array containing 0xE1 and 0x04 and I need to manipulate it into producing 0x104 and I'm struggling with it. I've been told to do the following:
- 0xE1 AND 0x1F = 0x01
- Rotate left 8 bits = 0x100
- 0x04 ADD 0x100 = 0x104
The raw instructions from the manufacturer:
- 0xE1 is greater than 0xE0
- 0xE1 AND 0x1F = 0x01
- Rotate left 8 bits = 0x100
- Read next byte = 0x04
- 0x04 Add 0x100 = 0x104
I can't get #2 to work. I've tried the extension methods from this SO question, the BitRotator.RotateLeft method, and the BitOperations.RotateLeft method, all without successfully getting 0x100. What am I supposed to do to get to 0x100?
I've never had to do bit-level operations before, so I'm struggling with it.
Update
I'm parsing a GPS report. There's fields in the report that are 1 or 2 bytes in size. If the value of byte 0 is less than 0xE0 then the size of the field is 1 byte. Else, the size is 2 bytes and I'm supposed to use the steps above to get to the real value. To quote the docs:
Any fields designated as 1-byte/2-byte fields are unsigned integers using the following encoding: If the 1st byte is less then 0xE0 (decimal 224) then the field size is 1-byte and the value is the value of the entire byte. If the 1st byte is greater-or-equal to 0xE0 then the field size is 2-bytes and the value is the value of the lsb 13-bits of the 2-byte field (most-significant byte first).
Here's the method I'm using (prototyping in LINQPad):
private static OneTwoField<Type> GetItemType(
ReadOnlySpan<byte> itemBytes) {
if (itemBytes[0] < 224) {
return new OneTwoField<Type>(1, (Type)itemBytes[0]);
}
itemBytes[0..2].DumpToTable();
// What do I do to get to the real value?
var type = ???;
return new OneTwoField<Type>(2, type);
}
The DumpToTable() extension produces the following:
Here's the OneTwoField<T>:
public struct OneTwoField<T> {
public OneTwoField(
byte size,
T value) {
Offset = size == 1 ? 0 : 1;
Size = size;
Value = value;
}
public int Offset { get; }
public byte Size { get; }
public T Value { get; }
}
And here's the Type:
[Flags]
public enum Type :
short {
General = 0x00,
Versions = 0x104
}
