探探大眼表情啥意思:在VB中编程,内容为:由十进制转成二进制

来源:百度文库 编辑:高考问答 时间:2024/05/13 03:24:45
写出程序代码

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)为错误语句。

给你一个任意进制之间的转换
你可以用
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

除2取余,一直除到小于2

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

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