1,十进制转十六进制
Function Dec2Hex(value As String) As String
value = Trim$(value)
If Len(value) = 0 Then
Dec2Hex = ""
Else
Dec2Hex = Hex(value)
End If
If Len(Dec2Hex) = 1 Then
Dec2Hex = "0" & Dec2Hex
ElseIf Len(Dec2Hex) <> 2 Then
Dec2Hex = "ERR"
MsgBox ("変数の長さはただしくない")
End If
End Function
value = Trim$(value)
If Len(value) = 0 Then
Dec2Hex = ""
Else
Dec2Hex = Hex(value)
End If
If Len(Dec2Hex) = 1 Then
Dec2Hex = "0" & Dec2Hex
ElseIf Len(Dec2Hex) <> 2 Then
Dec2Hex = "ERR"
MsgBox ("変数の長さはただしくない")
End If
End Function
2,十六进制转十进制
Function Hex2Dec(value As String) As Byte
value = Trim$(value)
If Len(value) = 0 Then
MsgBox "error"
Exit Function
End If
Hex2Dec = CByte("&H" & value)
End Function
value = Trim$(value)
If Len(value) = 0 Then
MsgBox "error"
Exit Function
End If
Hex2Dec = CByte("&H" & value)
End Function
2,十进制转二进制
以下VB函数可以完成十进制转换二进制的工作。另外,这个函数还加入了对二进制长度的判断,如果转换出来的二进制长度低于最小值,函数会自动在二进制字符串前补0。
Public Function DecimalToBinary(DecimalValue As Long, MinimumDigits As Integer)
As String
' Returns a string containing the binary
' representation of a positive integer.
Dim result As String
Dim ExtraDigitsNeeded As Integer
' Make sure value is not negative.
DecimalValue = Abs(DecimalValue)
' Construct the binary value.
Do
result = CStr(DecimalValue Mod 2) & result
DecimalValue = DecimalValue 2
Loop While DecimalValue > 0
' Add leading zeros if needed.
ExtraDigitsNeeded = MinimumDigits - Len(result)
If ExtraDigitsNeeded > 0 Then
result = String(ExtraDigitsNeeded, "0") & result
End If
DecimalToBinary = result
End Function
As String
' Returns a string containing the binary
' representation of a positive integer.
Dim result As String
Dim ExtraDigitsNeeded As Integer
' Make sure value is not negative.
DecimalValue = Abs(DecimalValue)
' Construct the binary value.
Do
result = CStr(DecimalValue Mod 2) & result
DecimalValue = DecimalValue 2
Loop While DecimalValue > 0
' Add leading zeros if needed.
ExtraDigitsNeeded = MinimumDigits - Len(result)
If ExtraDigitsNeeded > 0 Then
result = String(ExtraDigitsNeeded, "0") & result
End If
DecimalToBinary = result
End Function
运营不易,
感谢支持!
我的微信
我的微信公众号
我的微信公众号扫一扫
我的公众号