那个方法首先要使用两个Function:
Function bLength(x As Long) As Long 注释:用来得到要转换的数字转换后的二进制长度
bLength = Int(Log(x) / Log(2))
End Function
Function bBit(x As Long, Pos As Long) As String 注释:得到指定位的字符
Dim lTmp As Long
lTmp = 2 ^ Pos
bBit = IIf((x And lTmp) = lTmp, "1", "0")
End Function
以下是主程序:
Function DecToBin(x As Long) As String
Dim sBuf As String, lLength As Long, lPos As Long
sBuf = ""
lLength = bLength(x)
For lPos = 0 To lLength
sBuf = bBit(x, lPos) & sBuf
Next
DecToBin = sBuf
sBuf = ""
End Function
x是要转换的Dec,返回值就是转换后的Bin了.
不过只是无聊的时候写的,所以也就没有研究相关的反函数和其它的转换方法.
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/