vb题目 将十进制转化为二进制???
发布网友
发布时间:2022-05-01 08:03
我来回答
共3个回答
热心网友
时间:2022-06-26 11:47
以下是我自己编写的程序,不管text1有多少位数,都可以转换成二进制!
Private Sub Command1_Click()
a = Text1.Text
b = DtoB(a)
Text2.Text = b
End Sub
Function Two(s)
p = Mid(s, Len(s), 1)
Two = CDbl(p Mod 2)
End Function
Function Cf(s)
For i = 1 To Len(s) Step 1
p = Mid(s, i, 1)
m = m & CDbl(o & p) \ 2
o = CDbl(o & p) Mod 2
Next
Cf = m
End Function
Function DtoB(s)
a = s
Do
i = Two(a)
n = i & n
a = Cf(a)
Loop Until CDbl(a) = 0
DtoB = n
End Function
热心网友
时间:2022-06-26 11:47
首先你犯了两个错误……
第一,Private Function DtoB(ByVal n As Long) As String传递时应该按值传递,而不是地址来传递的
第二,你已经定义了ByVal n As Long,在下面就不应该在定义了。
改过后的程序
Private Sub Command1_Click()
Dim a, b As Long
a = Val(Text1.Text)
d = DtoB(a)
Text2.Text = d
End Sub
Private Function DtoB(ByVal n As Long) As String
Dim b As Variant
c = ""
Do While n > 0
b = n Mod 2
c = b & c
n = Int(n / 2)
Loop
DtoB = c
End Function
热心网友
时间:2022-06-26 11:48
Private Sub Command1_Click()
Dim a, b As Long
a = Val(Text1.Text)
d = DtoB(a)
Text2.Text = d
End Sub
Private Function DtoB(ByVal n As Long) As String
Dim b As Variant
c = ""
Do While n > 0
b = n Mod 2
c = b & c
n = Int(n / 2)
Loop
DtoB = c
End Function