VB程序代码,编制一个将十进制整数转换为二进制数的程序。

VB程序代码,编制一个将十进制整数转换为二进制数的程序。
2024-12-29 22:18:25
推荐回答(5个)
回答1:

用途:将十进制转化为二 进 制
' 输入:Dec(十进制数)
' 输入数据类型:Long
' 输出:DEC_to_BIN(二 进 制数)
' 输出数据类型:String
' 输入的最大数为2147483647,输出最大数为1111111111111111111111111111111(31个1)
Public Function DEC_to_BIN(Byval Dec As Long) As String
DEC_to_BIN = ""
Do While Dec > 0
DEC_to_BIN = Dec Mod 2 & DEC_to_BIN
Dec = Dec \ 2
Loop
End Function

Private Sub Command1_Click()
Dim a As String
Dim b As Long 'b是你要转换的数
a = DEC_to_BIN(b)
End Sub

回答2:

一、函数代码:
Private Function C10to2(n10 As Long) As String
Dim n2 As String
Do Until n10 = 0
n2 = n10 Mod 2 & n2
n10 = n10 \ 2
Loop
C10to2 = n2
End Function

二、调用举例:
dim i as long,s as string
i=1456
s=C10to2(i)
表示将10进制1456转换为2进制:s=10110110000

回答3:

Private Sub command1_click()
Dim x$, n%
n = 20
Do While n <> 0
a = n Mod 2
n = n \ 2
x = Chr(48 + a) & x
Loop
Print x
End Sub

回答4:

网页链接这种问题以后就不要问了,百度一大推答案!

回答5:

Private Sub command1_click()
Dim x$, n%
n = Text1.Text
Do While n <> 0
a = n Mod 2
n = n \ 2
x = Chr(48 + a) & x
Loop
Text2.Text = x
End Sub