|
本帖最后由 wang1999 于 2015-8-25 19:05 编辑
这是我刚开始学VBA时写的, 结构清晰, 看着清爽. 还算不错. 只是性能方面,现在看来不敢恭维。- Public Function BinToHex(ByVal Bin As String) As String
- Dim i As Long
- i = Len(Bin) Mod 4
- If i <> 0 Then '按每组4个二进制字符配齐
- Bin = String(4 - i, "0") & Bin
- End If
- For i = 1 To Len(Bin) Step 4
- Select Case Mid$(Bin, i, 4)
- Case "0000": BinToHex = BinToHex & "0"
- Case "0001": BinToHex = BinToHex & "1"
- Case "0010": BinToHex = BinToHex & "2"
- Case "0011": BinToHex = BinToHex & "3"
- Case "0100": BinToHex = BinToHex & "4"
- Case "0101": BinToHex = BinToHex & "5"
- Case "0110": BinToHex = BinToHex & "6"
- Case "0111": BinToHex = BinToHex & "7"
- Case "1000": BinToHex = BinToHex & "8"
- Case "1001": BinToHex = BinToHex & "9"
- Case "1010": BinToHex = BinToHex & "A"
- Case "1011": BinToHex = BinToHex & "B"
- Case "1100": BinToHex = BinToHex & "C"
- Case "1101": BinToHex = BinToHex & "D"
- Case "1110": BinToHex = BinToHex & "E"
- Case "1111": BinToHex = BinToHex & "F"
- End Select
- Next
- End Function
复制代码
下面这个是我的VBA最终版,带错误检查, 带占位符的. 测后速度 191967.953
Public Function BinToHexV5(BinString As String, Optional ByVal Places As Long = 8) As String
Dim i As Long, lLen As Long
Static sacurBinStr(1 To 8) As Currency, aiRetStr(1 To 8) As Integer, sai0Str(1 To 8) As Integer
'//1.初始化
'初始化字符串=8个"0" '判断语句不作比较时, Integer 比 long 要快
If sai0Str(1) Then Else sai0Str(1) = 48: sai0Str(2) = 48: sai0Str(3) = 48: sai0Str(4) = 48: sai0Str(5) = 48: sai0Str(6) = 48: sai0Str(7) = 48: sai0Str(8) = 48
CopyMem16 aiRetStr(1), sai0Str(1)
'参数检查
lLen = LenB(BinString) '后面复制时单位为字节, 所以为 LenB
If lLen > 64& Then Err.Raise erCom + 3, "BinToHex", "参数过长": Exit Function
If lLen = 0& Then Err.Raise erCom + 4, "BinToHex", "空参数": Exit Function
i = (lLen + 7&) \ 8& '取得 BinString 的16进制最高位的位置
If Places < 1& Or Places > 8& Then Places = i '超出范围时, 重定占位长度
'复制 参数(BinString)到数组; VarPtr(sacurBinStr(8)) - lLen + 8: 字节对齐;
sacurBinStr(9 - i) = 1351100504368.7472@ '="0000", 将 BinString 按4位一组在高位补齐前导"0"字符串
MoveMemoryPtr2Ptr VarPtr(sacurBinStr(8)) - lLen + 8, BinString, lLen
'//2.转换数据. 遍历字符串, 从高位开始转换; '处理 Places, 如果设置长度过小或过大, 则输出实际位数
For i = 9 - i To 8
Select Case sacurBinStr(i)
Case 1351100504368.7472@ '0000
aiRetStr(i) = 48 ' "0"
Case 1379248002039.8128@ '0001
aiRetStr(i) = 49 ' "1"
Case 1351100933865.4768@ '0010
aiRetStr(i) = 50 ' "2"
Case 1379248431536.5424@ '0011
aiRetStr(i) = 51 ' "3"
Case 1351100504375.3008@ '0100
aiRetStr(i) = 52 ' "4"
Case 1379248002046.3664@ '0101
aiRetStr(i) = 53 ' "5"
Case 1351100933872.0304@ '0110
aiRetStr(i) = 54 ' "6"
Case 1379248431543.096@ '0111
aiRetStr(i) = 55 ' "7"
Case 1351100504368.7473@ '1000
aiRetStr(i) = 56 ' "8"
Case 1379248002039.8129@ '1001
aiRetStr(i) = 57 ' "9"
Case 1351100933865.4769@ '1010
aiRetStr(i) = 65 ' "A"
Case 1379248431536.5425@ '1011
aiRetStr(i) = 66 ' "B"
Case 1351100504375.3009@ '1100
aiRetStr(i) = 67 ' "C"
Case 1379248002046.3665@ '1101
aiRetStr(i) = 68 ' "D"
Case 1351100933872.0305@ '1110
aiRetStr(i) = 69 ' "E"
Case 1379248431543.0961@ '1111
aiRetStr(i) = 70 ' "F"
Case Else
Err.Raise erCom + 5, "BinToHexV5", "类型不符合": Exit Function
End Select
Next
'返回数据
BinToHexV5 = SysAllocStringLen(aiRetStr(1), Places)
End Function
|
|