vb中如何十进制转换二进制,代码怎么写?
答案:2 悬赏:30 手机版
解决时间 2021-03-06 13:10
- 提问者网友:暗中人
- 2021-03-06 02:33
vb中如何十进制转换二进制,代码怎么写?
最佳答案
- 五星知识达人网友:雾月
- 2021-03-06 02:46
Option Explicit
Dim sBin As String
Dim sHex As String
Private Sub Command1_Click()
sHex = Text1
HexToBin (sHex)
Text2 = sBin
sBin = ""
End Sub
Private Sub Form_Load()
Text1 = ""
End Sub
Public Function HexToBin(ByVal sHex As String) As String '处理16进制转换2进制代码
Const s1 = "0000101001101111000", s2 = "0125A4936DB7FEC8"
Dim i As Integer
sHex = UCase(sHex)
For i = 1 To Len(sHex)
sBin = sBin & Mid(s1, InStr(1, s2, Mid(sHex, i, 1)), 4)
Next i
End Function
Dim sBin As String
Dim sHex As String
Private Sub Command1_Click()
sHex = Text1
HexToBin (sHex)
Text2 = sBin
sBin = ""
End Sub
Private Sub Form_Load()
Text1 = ""
End Sub
Public Function HexToBin(ByVal sHex As String) As String '处理16进制转换2进制代码
Const s1 = "0000101001101111000", s2 = "0125A4936DB7FEC8"
Dim i As Integer
sHex = UCase(sHex)
For i = 1 To Len(sHex)
sBin = sBin & Mid(s1, InStr(1, s2, Mid(sHex, i, 1)), 4)
Next i
End Function
全部回答
- 1楼网友:平生事
- 2021-03-06 03:08
=====================================================================
算法:
二进制转十进制
从最后一位开始算,依次列为第0、1、2...位
第n位的数(0或1)乘以2的n次方
得到的结果相加就是答案
例如:01101011.转十进制:
第0位:1乘2的0次方=1
1乘2的1次方=2
0乘2的2次方=0
1乘2的3次方=8
0乘2的4次方=0
1乘2的5次方=32
1乘2的6次方=64
0乘2的7次方=0
然后:1+2+0
+8+0+32+64+0=107.
二进制01101011=十进制107.
代码:
public function bin_to_dec(byval bin as string) as long
dim i as long
for i = 1 to len(bin)
bin_to_dec = bin_to_dec * 2 + val(mid(bin, i, 1))
next i
end function
private sub command1_click()
m = inputbox("")
msgbox bin_to_dec(m)
end sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯