注册 登录
编程论坛 VB6论坛

请问各位高手,在VB上怎么把“点分十进制”的ip转换成二进制??谢谢

cattac 发布于 2013-04-02 17:51, 652 次点击
现在的很简单,只是把一个十进制的数字转换成二进制

Private Sub Command1_Click()
a = Val(Text1.Text)
b = ""
Do While a <> 0
   b = (a Mod 2) & b
   a = a \ 2
Loop
Text2.Text = Format(b, "00000000")
End Sub

请问怎么可以输入“192.168.1.1”这样,一起转换成二进制,谢谢高手们。
2 回复
#2
bczgvip2013-04-02 20:41
为嘛转二进制?不是十六么?
还有,是三十二位IP地址转二进制,还是字符串转?
#3
lowxiong2013-04-02 22:45
Private Sub Command1_Click()
  Dim b() As String, i As Integer, j As Integer, k As Integer
  b = Split(Text1, ".")
  Text2 = ""
  For i = 0 To UBound(b)
    j = Val(b(i))
    b(i) = ""
    For k = 0 To 7
      b(i) = (j And 2 ^ k) \ 2 ^ k & b(i)
    Next
    Text2 = Text2 & b(i) & "."
  Next
  If Text2 <> "" Then Text2 = Left(Text2, Len(Text2) - 1)
End Sub


[ 本帖最后由 lowxiong 于 2013-4-2 22:52 编辑 ]
1