Afan的代码挺绕,上个简单的。Dim $str = "111"
Dim $str1 = "111111111111111111111"
MsgBox(0, "2进制转16进制", BIN_to_HEX($str) & @CRLF & BIN_to_HEX($str1))
Func BIN_to_HEX($Bin)
Local $i, $Hex
If Mod(StringLen($Bin), 4) <> 0 Then
For $i = 1 To 4 - Mod(StringLen($Bin), 4)
$Bin = "0" & String($Bin)
Next
EndIf
For $i = 1 To StringLen($Bin) Step 4
Switch StringMid($Bin, $i, 4)
Case "0000"
$Hex &= "0"
Case "0001"
$Hex &= "1"
Case "0010"
$Hex &= "2"
Case "0011"
$Hex &= "3"
Case "0100"
$Hex &= "4"
Case "0101"
$Hex &= "5"
Case "0110"
$Hex &= "6"
Case "0111"
$Hex &= "7"
Case "1000"
$Hex &= "8"
Case "1001"
$Hex &= "9"
Case "1010"
$Hex &= "A"
Case "1011"
$Hex &= "B"
Case "1100"
$Hex &= "C"
Case "1101"
$Hex &= "D"
Case "1110"
$Hex &= "E"
Case "1111"
$Hex &= "F"
EndSwitch
Next
$Hex = StringRegExpReplace($Hex, "^0*", "")
Return $Hex
EndFunc ;==>BIN_to_HEX
|