Public Function Hex2Bin(InputData As String) As String Dim i As Integer Dim BinOut As String Dim Lenhex As Integer InputData = UCase(InputData) Lenhex = Len(InputData)
For i = 1 To Lenhex
If IsNumeric(Mid(InputData, i, 1)) Then GoTo NumOk ElseIf Mid(InputData, i, 1) = "A" Then GoTo NumOk ElseIf Mid(InputData, i, 1) = "B" Then GoTo NumOk ElseIf Mid(InputData, i, 1) = "C" Then GoTo NumOk ElseIf Mid(InputData, i, 1) = "D" Then GoTo NumOk ElseIf Mid(InputData, i, 1) = "E" Then GoTo NumOk ElseIf Mid(InputData, i, 1) = "F" Then GoTo NumOk Else MsgBox "Number given is not in Hex format", vbCritical Exit Function End If
NumOk: Next i
BinOut = "" For i = 1 To Lenhex
If Mid(InputData, i, 1) = "0" Then BinOut = BinOut + "0000" ElseIf Mid(InputData, i, 1) = "1" Then BinOut = BinOut + "0001" ElseIf Mid(InputData, i, 1) = "2" Then BinOut = BinOut + "0010" ElseIf Mid(InputData, i, 1) = "3" Then BinOut = BinOut + "0011" ElseIf Mid(InputData, i, 1) = "4" Then BinOut = BinOut + "0100" ElseIf Mid(InputData, i, 1) = "5" Then BinOut = BinOut + "0101" ElseIf Mid(InputData, i, 1) = "6" Then BinOut = BinOut + "0110" ElseIf Mid(InputData, i, 1) = "7" Then BinOut = BinOut + "0111" ElseIf Mid(InputData, i, 1) = "8" Then BinOut = BinOut + "1000" ElseIf Mid(InputData, i, 1) = "9" Then BinOut = BinOut + "1001" ElseIf Mid(InputData, i, 1) = "A" Then BinOut = BinOut + "1010" ElseIf Mid(InputData, i, 1) = "B" Then BinOut = BinOut + "1011" ElseIf Mid(InputData, i, 1) = "C" Then BinOut = BinOut + "1100" ElseIf Mid(InputData, i, 1) = "D" Then BinOut = BinOut + "1101" ElseIf Mid(InputData, i, 1) = "E" Then BinOut = BinOut + "1110" ElseIf Mid(InputData, i, 1) = "F" Then BinOut = BinOut + "1111" Else MsgBox "错误", vbCritical End If Next i Hex2Bin = BinOut eds: End Function
用函数Oct()将十进制位数转换为八进制,用Hex()将十进制位数转换为十六进制,不过值得注意的是转换后数字型变为字符型。如果是一个变量我们可以用如下代码完成十进制向其他进制转换的目的。 dim ANumaslong ANum=&O10′&O是八进制的表示符号ANum自行转换为8。 ANum=&HA′&H是十六进制的表示符号ANum自行转换为10。 如果我们有一个字符变量Astring为八进制的或十六进制的,用ANum=″&O″+Astring和ANum=″&H″+Astring能将它转换为十进制。 VB中我没有找到二进制的转换函数,用如下代码可以实现十进制到二进制的转换。 OptionExplicit Private Function TenturnTwo(ByValvarNumAsLong) Dim returnStringAsString,ModNumAsInteger Do WhilevarNum>0 ModNum=varNumMod2 varNum=varNum\2 returnString=Trim(Str(ModNum))+returnString Loop TenturnTwo=returnString End Function Private Function TwoturnTen(ByValvarStringAsString) DimSLenAsLong,I As Long,returnNumAsLong SLen=Len(varString) ForI=0ToSLen-1 returnNum=returnNum+Val(Mid(varString,I+1,1))*(2^(SLen-I-1)) Next TwoturnTen=returnNum End Function
Public Function Bin2Hex(InputData As String) As String Dim i As Integer Dim LenBin As Integer Dim JOne As String Dim NumBlocks As Integer Dim FullBin As String Dim HexOut As String Dim TempBinBlock As String Dim TempHex As String
LenBin = Len(InputData)
For i = 1 To LenBin JOne = Mid(InputData, i, 1) If JOne <> "0" And JOne <> "1" Then MsgBox "NOT A BINARY NUMBER", vbCritical Exit Function End If Next i
FullBin = InputData If LenBin < 4 Then If LenBin = 3 Then FullBin = "0" + FullBin ElseIf LenBin = 2 Then FullBin = "00" + FullBin ElseIf LenBin = 1 Then FullBin = "000" + FullBin ElseIf LenBin = 0 Then MsgBox "Nothing Given..", vbCritical Exit Function End If NumBlocks = 1 GoTo DoBlocks End If If LenBin = 4 Then NumBlocks = 1 GoTo DoBlocks End If If LenBin > 4 Then
Dim TempHold As Currency Dim TempDiv As Currency Dim AfterDot As Integer Dim Pos As Integer
Dim BinOut As String
Dim Lenhex As Integer
InputData = UCase(InputData)
Lenhex = Len(InputData)
For i = 1 To Lenhex
If IsNumeric(Mid(InputData, i, 1)) Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "A" Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "B" Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "C" Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "D" Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "E" Then
GoTo NumOk
ElseIf Mid(InputData, i, 1) = "F" Then
GoTo NumOk
Else
MsgBox "Number given is not in Hex format", vbCritical
Exit Function
End If
NumOk:
Next i
BinOut = ""
For i = 1 To Lenhex
If Mid(InputData, i, 1) = "0" Then
BinOut = BinOut + "0000"
ElseIf Mid(InputData, i, 1) = "1" Then
BinOut = BinOut + "0001"
ElseIf Mid(InputData, i, 1) = "2" Then
BinOut = BinOut + "0010"
ElseIf Mid(InputData, i, 1) = "3" Then
BinOut = BinOut + "0011"
ElseIf Mid(InputData, i, 1) = "4" Then
BinOut = BinOut + "0100"
ElseIf Mid(InputData, i, 1) = "5" Then
BinOut = BinOut + "0101"
ElseIf Mid(InputData, i, 1) = "6" Then
BinOut = BinOut + "0110"
ElseIf Mid(InputData, i, 1) = "7" Then
BinOut = BinOut + "0111"
ElseIf Mid(InputData, i, 1) = "8" Then
BinOut = BinOut + "1000"
ElseIf Mid(InputData, i, 1) = "9" Then
BinOut = BinOut + "1001"
ElseIf Mid(InputData, i, 1) = "A" Then
BinOut = BinOut + "1010"
ElseIf Mid(InputData, i, 1) = "B" Then
BinOut = BinOut + "1011"
ElseIf Mid(InputData, i, 1) = "C" Then
BinOut = BinOut + "1100"
ElseIf Mid(InputData, i, 1) = "D" Then
BinOut = BinOut + "1101"
ElseIf Mid(InputData, i, 1) = "E" Then
BinOut = BinOut + "1110"
ElseIf Mid(InputData, i, 1) = "F" Then
BinOut = BinOut + "1111"
Else
MsgBox "错误", vbCritical
End If
Next i
Hex2Bin = BinOut
eds:
End Function
dim ANumaslong
ANum=&O10′&O是八进制的表示符号ANum自行转换为8。
ANum=&HA′&H是十六进制的表示符号ANum自行转换为10。
如果我们有一个字符变量Astring为八进制的或十六进制的,用ANum=″&O″+Astring和ANum=″&H″+Astring能将它转换为十进制。
VB中我没有找到二进制的转换函数,用如下代码可以实现十进制到二进制的转换。
OptionExplicit
Private Function TenturnTwo(ByValvarNumAsLong)
Dim returnStringAsString,ModNumAsInteger
Do WhilevarNum>0
ModNum=varNumMod2
varNum=varNum\2
returnString=Trim(Str(ModNum))+returnString
Loop
TenturnTwo=returnString
End Function
Private Function TwoturnTen(ByValvarStringAsString)
DimSLenAsLong,I As Long,returnNumAsLong
SLen=Len(varString)
ForI=0ToSLen-1
returnNum=returnNum+Val(Mid(varString,I+1,1))*(2^(SLen-I-1))
Next
TwoturnTen=returnNum
End Function
这里有好多例子,去看看先。
Dim LenBin As Integer
Dim JOne As String
Dim NumBlocks As Integer
Dim FullBin As String
Dim HexOut As String
Dim TempBinBlock As String
Dim TempHex As String
LenBin = Len(InputData)
For i = 1 To LenBin
JOne = Mid(InputData, i, 1)
If JOne <> "0" And JOne <> "1" Then
MsgBox "NOT A BINARY NUMBER", vbCritical
Exit Function
End If
Next i
FullBin = InputData If LenBin < 4 Then
If LenBin = 3 Then
FullBin = "0" + FullBin
ElseIf LenBin = 2 Then
FullBin = "00" + FullBin
ElseIf LenBin = 1 Then
FullBin = "000" + FullBin
ElseIf LenBin = 0 Then
MsgBox "Nothing Given..", vbCritical
Exit Function
End If
NumBlocks = 1
GoTo DoBlocks
End If If LenBin = 4 Then
NumBlocks = 1
GoTo DoBlocks
End If If LenBin > 4 Then
Dim TempHold As Currency
Dim TempDiv As Currency
Dim AfterDot As Integer
Dim Pos As Integer
TempHold = Len(InputData)
TempDiv = (TempHold / 4)
Pos = InStr(1, CStr(TempDiv), ".")
If Pos = 0 Then
NumBlocks = TempDiv
GoTo DoBlocks
End If
AfterDot = Mid(CStr(TempDiv), (Pos + 1))
If AfterDot = 25 Then
FullBin = "000" + FullBin
NumBlocks = (Len(FullBin) / 4)
ElseIf AfterDot = 5 Then
FullBin = "00" + FullBin
NumBlocks = (Len(FullBin) / 4)
ElseIf AfterDot = 75 Then
FullBin = "0" + FullBin
NumBlocks = (Len(FullBin) / 4)
Else
MsgBox "Big Time Screw up happened, WAHHHHHHHHHHH", vbInformation
Exit Function
End If
GoTo DoBlocks
End If
DoBlocks:
HexOut = ""
For i = 1 To Len(FullBin) Step 4
TempBinBlock = Mid(FullBin, i, 4)
If TempBinBlock = "0000" Then
HexOut = HexOut + "0"
ElseIf TempBinBlock = "0001" Then
HexOut = HexOut + "1"
ElseIf TempBinBlock = "0010" Then
HexOut = HexOut + "2"
ElseIf TempBinBlock = "0011" Then
HexOut = HexOut + "3"
ElseIf TempBinBlock = "0100" Then
HexOut = HexOut + "4"
ElseIf TempBinBlock = "0101" Then
HexOut = HexOut + "5"
ElseIf TempBinBlock = "0110" Then
HexOut = HexOut + "6"
ElseIf TempBinBlock = "0111" Then
HexOut = HexOut + "7"
ElseIf TempBinBlock = "1000" Then
HexOut = HexOut + "8"
ElseIf TempBinBlock = "1001" Then
HexOut = HexOut + "9"
ElseIf TempBinBlock = "1010" Then
HexOut = HexOut + "A"
ElseIf TempBinBlock = "1011" Then
HexOut = HexOut + "B"
ElseIf TempBinBlock = "1100" Then
HexOut = HexOut + "C"
ElseIf TempBinBlock = "1101" Then
HexOut = HexOut + "D"
ElseIf TempBinBlock = "1110" Then
HexOut = HexOut + "E"
ElseIf TempBinBlock = "1111" Then
HexOut = HexOut + "F"
End If
Next i
Bin2Hex = HexOut
End Function