VB中十进制转换为二进制的代码
4个回答
展开全部
可以实现的方法很多。最基本的有递归和非递归的做法。
递归的做法:
Private Sub Command1_Click()
n = CLng(InputBox("N="))
Print n; "=(";
dec2bin (n)
Print ")2"
End Sub
Public Sub dec2bin(x As Long)
If x > 1 Then dec2bin (x \ 2)
Print Trim(Str(x Mod 2));
End Sub
非递归则是利用数组:
Private Sub Command1_Click()
n = CLng(InputBox("N="))
s = n & "=("
s1 = ""
While n > 0
s1 = n Mod 2 & s1
n = n \ 2
Wend
s = s & s1 & ")2"
Print s
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
X = InputBox("请输入一个数")
a = X
Do While (a > 0)
q = a Mod 2 & q
a = a \ 2
Loop
MsgBox (q)
a = X
Do While (a > 0)
q = a Mod 2 & q
a = a \ 2
Loop
MsgBox (q)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
'十进转二进
Public Function D2B(Dec As Long) As String
D2B = ""
Do While Dec > 0
D2B = Dec Mod 2 & D2B
Dec = Dec \ 2
Loop
End Function
Public Function D2B(Dec As Long) As String
D2B = ""
Do While Dec > 0
D2B = Dec Mod 2 & D2B
Dec = Dec \ 2
Loop
End Function
追问
Dim m As Integer, n As Integer
Dim Ch As String, i As String, b As String
m = Val(Text1.Text)
Ch = "01"
Do While m 0
n = m / 2
i = m Mod 2
m = n
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询