我正在尝试与需要汉明编码的ASCII字符的RS232设备通信.
下表由制造商提供:
Byte Encoded
0 15
1 02
2 49
3 5E
4 64
5 73
6 38
7 2F
8 D0
9 C7
A 8C
B 9B
C A1
D B6
E FD
F EA
我编写了这个C#函数来编码每个字节(ascii char),但设备只在其屏幕上解析行话.
/// <summary>Takes ASCII char as byte and returns hamming encoded version.</summary>
/// <param name="input">Byte to encode.</param>
/// <returns>Hamming encoded byte.</returns>
private byte ByteHamming(byte input)
{
switch (input)
{
case 0x00:
return 0x15;
case 0x01:
return 0x02;
case 0x02:
return 0x49;
case 0x03:
return 0x5E;
case 0x04:
return 0x64;
case 0x05:
return 0x73;
case 0x06:
return 0x38;
case 0x07:
return 0x2F;
case 0x08:
return 0xD0;
case 0x09:
return 0xC7;
case 0x0A:
return 0x8C;
case 0x0B:
return 0x9B;
case 0x0C:
return 0xA1;
case 0x0D:
return 0xB6;
case 0x0E:
return 0xFD;
case 0x0F:
return 0xEA;
default:
return input;
}
}
我误解汉明应该如何运作?
我不是计算机科学家:)
最佳答案 正如@Mitch建议的那样,你应该对半字节进行编码.所以像这样的东西应该工作:
将实际方法重命名为NibbleHamming(),并添加:
private byte ByteHamming(byte input)
{
byte lo = (byte)(input & 0x0F);
byte hi = (byte)((input & 0xF0) >> 4);
lo = NibbleHamming(lo);
hi = NibbleHamming(hi);
return lo + hi * 0x10;
}