在VB中编程,内容为:由十进制转成二进制

写出程序代码... 写出程序代码 展开
 我来答
百度网友0f6942b9f
2005-12-22 · 超过36用户采纳过TA的回答
知道答主
回答量:132
采纳率:0%
帮助的人:125万
展开全部
Dim i As Integer
Dim s As String
Dim m As Integer

i = 7
s = ""
Do While i <> 0
m = i Mod 2
s = CStr(m) & s
i = i \ 2
Loop
msgbox s

二楼的s=s & CStr(m)为错误语句。
遗古大于斯9495
2005-12-22 · TA获得超过140个赞
知道答主
回答量:361
采纳率:0%
帮助的人:0
展开全部
给你一个任意进制之间的转换
你可以用
jzzh ( "7789.101",10,2)

Public Function jzzh(ByVal s As String, ByVal sfrom As Long, ByVal sto As Long) As String
Dim xsstr As String
Dim zsstr As String
If Mid(s, 1, 1) = "-" Then
fh = -1
Else
fh = 1
End If
If fh < 0 Then s = Mid(s, 2)
i = 1
Do Until i = Len(s) '去掉非法字符
If getstr(Mid(s, i, 1)) < 0 And Mid(s, i, 1) <> "." Then
s = Mid(s, 1, i - 1) + Mid(s, i + 1)
Else
i = i + 1
End If
Loop
If InStr(1, s, ".") > 0 Then
xsstr = Mid(s, InStr(1, s, ".") + 1)
zsstr = Mid(s, 1, InStr(1, s, ".") - 1)

Else
zsstr = s
xsstr = ""
End If
zscount = Len(zsstr)
xscount = Len(xsstr)
ReDim Preserve zs(1 To zscount) As Long
If xscount > 0 Then ReDim Preserve xs(1 To xscount) As Long
For i = 1 To zscount
zs(i) = getstr(Mid(zsstr, i, 1))
Next i
For i = 1 To xscount
xs(i) = getstr(Mid(xsstr, i, 1))
Next i
zsdat = 0
For i = 1 To zscount
zsdat = zsdat + zs(i) * sfrom ^ (zscount - i)
Next i
xsdat = 0
For i = 1 To xscount
xsdat = xsdat + xs(i) * sfrom ^ (-i)
Next i
zsdats = ""
j = 0
Do Until zsdat < sto
zsdats = getlng(sMod(zsdat, sto)) + zsdats
zsdat = Int(zsdat / sto)
i = i + 1
Loop
zsdats = getlng(sMod(zsdat, sto)) + zsdats
If xscount > 0 Then
i = 0
xsdats = ""
Do Until i > 10
xsdats = xsdats + getlng(Int(xsdat * sto))
xsdat = xsdat * sto - Int(xsdat * sto)

i = i + 1
Loop

i = Len(xsdats)
Do Until Mid(xsdats, i, 1) <> "0"
i = i - 1
Loop
xsdats = Mid(xsdats, 1, i)
Else
xsdats = ""
End If

jzzh = zsdats
If xscount > 0 Then jzzh = jzzh + "." + xsdats
End Function
Public Function getstr(ByVal s As String) As Long
If Asc(s) >= Asc("0") And Asc(s) <= Asc("9") Then
getstr = Asc(s) - Asc("0")
ElseIf Asc(s) >= Asc("a") And Asc(s) <= Asc("z") Then
getstr = Asc(s) - Asc("a") + 10
ElseIf Asc(s) >= Asc("A") And Asc(s) <= Asc("Z") Then
getstr = Asc(s) - Asc("A") + 36
Else
getstr = -1
End If
End Function
Private Function sint(d As Variant) As Long
sint = Int(d)
If d > Int(d) + 0.5 Then sint = Int(d) + 1
End Function
Private Function getlng(ByVal s As Long) As String
If s >= 0 And s <= 9 Then
getlng = CStr(s)
ElseIf s >= 10 And s <= 35 Then
getlng = Chr(s - 10 + Asc("a"))
ElseIf s > 35 Then
getlng = Chr(s - 36 + Asc("A"))
End If
End Function
Private Function sMod(ByVal s As Double, ByVal a As Double) As Long
sMod = sint((s / a - Int(s / a)) * a)
End Function
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友d0a730732
2005-12-22 · TA获得超过3290个赞
知道小有建树答主
回答量:2630
采纳率:0%
帮助的人:1440万
展开全部
除2取余,一直除到小于2
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
依土天羽
2005-12-22 · TA获得超过289个赞
知道小有建树答主
回答量:337
采纳率:100%
帮助的人:402万
展开全部
Dim i As Integer
Dim s As String
Dim m As Integer

i = 7
s = ""
Do
m = i Mod 2
s = s & CStr(m)
i = i \ 2
Loop While i <> 0
Print s
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友8f1185cd3
2005-12-22 · TA获得超过920个赞
知道小有建树答主
回答量:1028
采纳率:0%
帮助的人:624万
展开全部
Private Sub Command1_Click() '输入按钮
Text1.SetFocus
End Sub

Private Sub Command2_Click() '输出按钮
d = Val(Text1.Text) '输入要转化的数字
Do While (d)
c = d Mod 2
d = d \ 2
er = c & er
Loop
Text2.Text = er '输出结果
End Sub

Private Sub Form_Load()
er = ""
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式