# FW: 补充范例码

Posted by 小呆 on May 09, 1999 at 00:07:16:

In Reply to: Re: 如何将字元转成二进位码 posted by yeong huann on May 07, 1999 at 17:27:47:

: 利用ASC(Y)得到89
: 再利用辗转相除法(用2除),取其余数,再将余数以字串处理
: 依序相加即可

Private Sub Command1_Click()
Debug.Print DecToBin("Y") '显示Y的二进位码
End Sub
Private Function DecToBin(a As String) As String
Dim temp As String
Dim i, s
temp = ""
s = Asc(a) '将字串转换成ascii码
Debug.Print "十进位="; s
Debug.Print "十六进位=0x"; Hex(s) '将字串转换成十六进位
For i = 7 To 0 Step -1
If s And 2 ^ i Then '判断其位元是否为1
temp = temp + "1"
Else
temp = temp + "0"
End If
Next i
Debug.Print "二进位="; temp
DecToBin = temp
End Function

Follow Ups: