①全0块“0000”,可以化简为“0”
②连续多个全0块,可以化简为“: :”
③一个IPv6地址中只能出现一个“: :”,出现多个全0块时,“: :” 要化简最长的一段,没有最长的就将最左侧的一段化简为“: :”
④“: :”可以出现在地址开头或结尾
如:数据“FBCD:EF01:6789”在化简前为“FBCD:EF01:0000:0000:0000:0000:0000:6789”。
小李根据上述化简算法设计了一个IP化简地址还原的VB程序,功能如下:在文本框Text1中输入已经化简的IP地址,单击“还原”按钮Command1,在文本框Text2中输出其化简前的IP地址。运行界面如图所示。
Function ipv6(s As String) As String
Dim i As Integer
For i= Len(s) + 1 To4
s= "0"+s
Next i
End Function
Private Sub Command1_Click( )
Dim ip(1 To 8) As String
Dim st As String
Dim f As Integer, k As Integer
Dim s As String, n As Integer
f= 0
s = Text1.Text
n= Len(s)
For i= 1 To n
c1 = Mid(s, i, 1)
If c1 <> "." Then
ip(k)= ip(k) +c1
End If
If c1= ":" Or i= n Then
If c2 =":" Then
f= k
Else
ip(k) = ipv6(ip(k))
k=k+ 1
End If
End If
Next i
k=k- 1
x=8-k
For i=k To f+ 1 Step-1
Next i
For i=f+ 1 To f+x
ip(i) = "0000"
Next i
Text2.Text= ip(1)
For i=2 To 8
Text2.Text = Text2.Text + ":" + ip(i)
Next i
End Sub