我想用一个字节替换我中那些神奇的 2 字节包中的List<byte>
每一个:
{ 0xF8, 0x00 } -> 替换为 0xF8
{ 0xF8, 0x01 } -> 替换为 0xFB
{ 0xF8, 0x02 } -> 替换为 0xFD
{ 0xF8, 0x03 } -> 替换为 0xFE
例如:
List<byte> message
= new List<byte> { 0xFF, 0xFF, 0xFB, 0xF8, 0x00, 0xF8, 0x01, 0xF8, 0x02, 0xF8, 0x03, 0xFE };
// will be converted to:
List<byte> expected
= new List<byte> { 0xFF, 0xFF, 0xFB, 0xF8, 0xFB, 0xFD, 0xFE, 0xFE };
到目前为止,这是我的解决方案,它有效,但我不喜欢它,因为它的可读性很差:
public static void RemoveEscapeSequences(List<byte> message)
{
// skipped parameter checks
for (int index = 0; index < message.Count - 1; ++index)
{
if (message[index] == 0xF8)
{
// found an escaped byte, look at the following byte to determine replacement
switch (message[index + 1])
{
case 0x0:
message[index] = 0xF8;
message.RemoveAt(index + 1);
break;
case 0x1:
message[index] = 0xFB;
message.RemoveAt(index + 1);
break;
case 0x2:
message[index] = 0xFD;
message.RemoveAt(index + 1);
break;
case 0x3:
message[index] = 0xFE;
message.RemoveAt(index + 1);
break;
}
}
}
}
是否有更短的解决方案以提高可读性?