VB 二进制,八进制,十进制,十六进制转换

[复制链接]
查看2623 | 回复2 | 2006-8-8 08:42:00 | 显示全部楼层 |阅读模式
VB 二进制,八进制,十进制,十六进制转换

Public Function D_To_B(ByVal Dec As Long) As String
Do
D_To_B = Dec Mod 2 & D_To_B
Dec = Dec \ 2
Loop While Dec
End Function

Public Function B_To_D(ByVal Bin As String) As Integer
Dim i As Long
For i = 1 To Len(Bin)
B_To_D = B_To_D * 2 + Val(m i d(Bin, i, 1))
Next i
End Function

Public Function H_To_B(ByVal Hex As String) As String
Dim i As Long
Dim B As String

Hex = UCase(Hex)
For i = 1 To Len(Hex)
s el e c t Case m i d(Hex, i, 1)
Case 0: B = B & 0000
Case 1: B = B & 0001
Case 2: B = B & 0010
Case 3: B = B & 0011
Case 4: B = B & 0100
Case 5: B = B & 0101
Case 6: B = B & 0110
Case 7: B = B & 0111
Case 8: B = B & 1000
Case 9: B = B & 1001
Case A: B = B & 1010
Case B: B = B & 1011
Case C: B = B & 1100
Case D: B = B & 1101
Case E: B = B & 1110
Case F: B = B & 1111
End s el e c t
Next i
While Left(B, 1) = 0
B = Right(B, Len(B) - 1)
Wend
H_To_B = B
End Function

Public Function B_To_H(ByVal Bin As String) As String
Dim i As Long
Dim H As String
If Len(Bin) Mod 4 <> 0 Then
Bin = String(4 - Len(Bin) Mod 4, 0) & Bin
End If

For i = 1 To Len(Bin) Step 4
s el e c t Case m i d(Bin, i, 4)
Case 0000: H = H & 0
Case 0001: H = H & 1
Case 0010: H = H & 2
Case 0011: H = H & 3
Case 0100: H = H & 4
Case 0101: H = H & 5
Case 0110: H = H & 6
Case 0111: H = H & 7
Case 1000: H = H & 8
Case 1001: H = H & 9
Case 1010: H = H & A
Case 1011: H = H & B
Case 1100: H = H & C
Case 1101: H = H & D
Case 1110: H = H & E
Case 1111: H = H & F
End s el e c t
Next i
B_To_H = H
End Function

[此贴子已经被loveplc于2007-1-14 22:58:59编辑过]
nickwang | 2006-11-26 01:47:00 | 显示全部楼层

感谢楼主!

zengwuhuiapi | 2007-12-31 18:03:00 | 显示全部楼层

[??]

请问有没有那位高手,给我一份VB与欧姆龙通讯及VB编程的怎么样读和写地址的相关资料.谢谢各位师父!!!

我的邮箱zengwuhS@yahoo.com.cn

您需要登录后才可以回帖 登录 | 注册哦

本版积分规则