vb二进制转十进制的函数?
展开全部
'返回二进制数对应的十进制数值
Public Function B2D(ByVal bstr As String) As Long
Dim n As Integer
Dim s As Long
Dim i As Integer
Dim w As Integer
n = Len(Trim(bstr)) '去掉字串首尾可能有的空格并测出字串的长度
s = 0 '累加器清零
For i = n To 1 Step -1 '依次取字串的各位
w = Val(Mid(bstr, i, 1)) '把每位字符转换为数值,非法字符将视为0
s = s + 2 ^ (n - i) * w '按位计权累加
Next i
B2D = s '返回累加值
End Function
'返回十进制数对应的二进制字串
Public Function D2B(ByVal Dnum As Long) As String
Dim xx As String
Dim yy As Integer
xx = "" '字串累加清空
Do While Dnum > 0 '循环取余至小于1
yy = Dnum Mod 2 '除2取余
Dec = Dec \ 2 '除2取整
xx = Trim(Str(yy)) & xx '字串累加
Loop
D2B = xx '返回字串
End Function
Public Function B2D(ByVal bstr As String) As Long
Dim n As Integer
Dim s As Long
Dim i As Integer
Dim w As Integer
n = Len(Trim(bstr)) '去掉字串首尾可能有的空格并测出字串的长度
s = 0 '累加器清零
For i = n To 1 Step -1 '依次取字串的各位
w = Val(Mid(bstr, i, 1)) '把每位字符转换为数值,非法字符将视为0
s = s + 2 ^ (n - i) * w '按位计权累加
Next i
B2D = s '返回累加值
End Function
'返回十进制数对应的二进制字串
Public Function D2B(ByVal Dnum As Long) As String
Dim xx As String
Dim yy As Integer
xx = "" '字串累加清空
Do While Dnum > 0 '循环取余至小于1
yy = Dnum Mod 2 '除2取余
Dec = Dec \ 2 '除2取整
xx = Trim(Str(yy)) & xx '字串累加
Loop
D2B = xx '返回字串
End Function
展开全部
函数是没有只有程序: Private Sub Form_Click() Dim Dec As Integer Dim Bin As String Dim res As Integer Dim i As Integer Dec = Val(InputBox("x=")) '输入一个十进制数 Form1.Print "十进制数:"; Dec Do res = Dec Mod 2 '求出除以2的余...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这是各进制间的转换。 满意采纳哈。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Public Function Bin_DEC(ByVal Dnum As String) As String
Dim xx As String
Dim n As Integer
Dim yy As Long
yy = 0
n = 0
For i = Len(Dnum) To 1 Step -1
xx = Mid(Dnum, i, 1)
If xx = "1" Or xx = "0" Then
yy = yy + Val(xx) * 2 ^ n
n = n + 1
Else
yy = 0
Exit For
End If
Next i
Bin_DEC = CStr(yy)
End Function
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询