每段海明码编码方法如下:
插入二进制位:在第1、2、4、8位置上插入4个二进制位r1、r2、r3、r4,初值为0。把原8位二进制数d1~d8从左往右依次填入剩余位置中,原数据位和插入的二进制位如图a所示:
图a
计算插入的校验位的值:把每个位置号转换成4位二进制数,选出形如“***1”(“*”表示一位0或1)的位置号,则统计这些位置号对应数据位中的“1”的个数,若是奇数,则r1为1,否则为0;再选出形如“**1*”的位置号,则统计这些位置号对应数据位中的“1”的个数,若是奇数,则r2为1,否则为0;以此类推……
如8位二进制数“10101101”的编码过程如下:
1)插入4个二进制位,初值为0,剩余位置依次填入原数据位,如图b所示。
图b
2)计算插入的校验位的值,选出形如“***1”的位置号,即1、3、5、7、9、11,统计这些位置号对应数据位中“1”的个数为2,故r1=0,同理可得:r2=1,r3=0,r4=1,如图c所示。
图c
3)按位置号从小到大依次拼接各数据位得到最后编码“011001011101”。
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer, m As Integer
Dim s As String, ch As String, result As String
Dim a(1 To 12) As Integer, n(1 To 4) As Integer
Dim b(1 To 8) As Integer
b(1) = 3: b(2) = 5: b(3) = 6: b(4) = 7
b(5) = 9: b(6) = 10: b(7) = 11: b(8) = 12 '编码数据的插入位置
s = Text1.Text: result = ""
For i = 1 To Len(s) \ 8
For j = 1 To 12
a(j) = 0
Next j
For j = 1 To 8
ch = Mid(s, (i - 1) * 8 + j, 1)
'把编码数据插入相应位置
Next j
For j = 1 To 4
n(j) = 0
Next j
For j = 1 To 12
m = j
For k = 1 To 4
If m Mod 2 = 1 Then
m = m \ 2
Next k
Next j
For j = 1 To 4
a(2 ^ (j - 1)) =
Next j
For j = 1 To 12
result = result + Chr(Asc("0") + a(j))
Next j
Next i
Text2.Text = result
End Sub