VB程序编写:二进制转换为十进制的函数

2025-01-20 03:03:58
推荐回答(2个)
回答1:

示例附件中


Private Sub Command1_Click()

Text2 = BinToDec(Text1)

End Sub


Private Function BinToDec(ejz As String) As Long

BinToDec = 0

Dim w As Long

For i = 0 To Len(ejz) - 1

If Mid(ejz, Len(ejz) - i, 1) = "1" Then

BinToDec = BinToDec + 2 ^ w

w = w + 1

ElseIf Mid(ejz, Len(ejz) - i, 1) = "0" Then

w = w + 1

End If

Next

End Function


回答2:

Private Sub Form_Load()
Debug.Print b2d("1101")
End Sub

Private Function b2d(src As String) As Integer
Dim i As Integer
Dim out As Integer

out = 0

For i = 1 To Len(src)
out = out * 2 + CInt(Mid(src, i, 1))
Next

b2d = out
End Function