十進制和二進制互換的兩個vb Function
'十進制轉二進制
Function dec2bin(mynum)
dim loopcounter
If mynum >= 2 ^ 31 Then
dec2bin = "Too big"
Exit Function
End If
Do
If (mynum And 2 ^ loopcounter) = 2 ^ loopcounter Then
dec2bin = "1" & dec2bin
Else
dec2bin = "0" & dec2bin
End If
loopcounter = loopcounter + 1
Loop Until 2 ^ loopcounter > mynum
End Function
'二進制轉十進制
Function bin2dec(mynum)
dim idx,tmp,result,digits
digits = Len(mynum)
For idx = digits To 1 Step -1
tmp = Mid(mynum, idx, 1)
If tmp = "1" Then result = result + 2 ^ (digits - idx)
Next
bin2dec = result
End Function
Function dec2bin(mynum)
dim loopcounter
If mynum >= 2 ^ 31 Then
dec2bin = "Too big"
Exit Function
End If
Do
If (mynum And 2 ^ loopcounter) = 2 ^ loopcounter Then
dec2bin = "1" & dec2bin
Else
dec2bin = "0" & dec2bin
End If
loopcounter = loopcounter + 1
Loop Until 2 ^ loopcounter > mynum
End Function
'二進制轉十進制
Function bin2dec(mynum)
dim idx,tmp,result,digits
digits = Len(mynum)
For idx = digits To 1 Step -1
tmp = Mid(mynum, idx, 1)
If tmp = "1" Then result = result + 2 ^ (digits - idx)
Next
bin2dec = result
End Function
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。