一个八进制和十六进制转二进制的奇妙方法

z_wenqian 2008-01-08 02:48:29
近来没事,翻看网上关于进制转换的文章,发现八进制与十六进制转二进制的方法都很麻烦,因此写个简单一点的方法,原理都一样,只是想法叫奇妙,感觉还比较好,抛出来供大家参考。

Public Function OctToBin(ByVal sOct As String) As String
Const s1 = "0001011100", s2 = "01253764"
Dim i As Integer, sBin As String
For i = 1 To Len(sOct)
sBin = sBin & Mid(s1, InStr(1, s2, Mid(sOct, i, 1)), 3)
Next i
OctToBin = sBin
End Function

Public Function HexToBin(ByVal sHex As String) As String
Const s1 = "0000101001101111000", s2 = "0125A4936DB7FEC8"
Dim i As Integer, sBin As String
sHex = UCase(sHex)
For i = 1 To Len(sHex)
sBin = sBin & Mid(s1, InStr(1, s2, Mid(sHex, i, 1)), 4)
Next i
HexToBin = sBin
End Function
...全文
6163 点赞 收藏 11
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
6742 2012-02-18
不错
谢谢!
回复
skdyu 2011-09-26
进制转换,学学
回复
wangxin5576 2009-08-17
学习学习
回复
pusiyu 2009-08-09
感觉满巧妙的
回复
z_wenqian 2009-04-20
我不知楼上的让我试什么,我记得好像试过才发的,是不是你弄错了
回复
lancelotaerin 2009-03-29
十六进制转二进制想法是不错,但是有BUG,不知试过没有,0-9这样的十六进制是可以转的,但是A——F就不行了
不信你试试看
回复
zcfhero 2008-03-26
看看.. bu不错
回复
z_wenqian 2008-01-09
相同方法,附上二进制转八进制和十六进制代码

Public Function BinToOct(ByVal sBin As String) As String
Const s1 = "0001011100", s2 = "01253764"
Dim i As Integer, sOCt As String
sBin = String(2 - (Len(sBin) - 1) Mod 3, "0") & sBin
For i = 1 To Len(sBin) Step 3
sOCt = sOCt & Mid(s2, InStr(1, s1, Mid(sBin, i, 3)), 1)
Next i
BinToOct = sOCt
End Function

Public Function BinToHex(ByVal sBin As String) As String
Const s1 = "0000101001101111000", s2 = "0125A4936DB7FEC8"
Dim i As Integer, sHex As String
sBin = String(3 - (Len(sBin) - 1) Mod 4, "0") & sBin
For i = 1 To Len(sBin) Step 4
sHex = sHex & Mid(s2, InStr(1, s1, Mid(sBin, i, 4)), 1)
Next i
BinToHex = sHex
End Function
回复
波导终结者 2008-01-08
想法比较巧妙,但是以前在哪见过几位高人讨论,说这样速度或者效率会受影响,不过自己没试过。
回复
tingting1123 2008-01-08
看看..
回复
awperpvip 2008-01-08
不错
xuexi
回复
发帖
VB基础类
创建于2007-09-28

7476

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-01-08 02:48
社区公告
暂无公告