bueno mira aca te paso un codigo haber si mas o menos vas viendo como es:
en un modulo(bas) pone esto:
Código:
Option Explicit
Public Numero As String
Public DeBase As Integer
Public tobase As Integer
Public Function HexToDec(ByVal HexStr As String) As Double
Dim mult As Double
Dim DecNum As Double
Dim ch As String
mult = 1
DecNum = 0
Dim i As Integer
For i = Len(HexStr) To 1 Step -1
ch = Mid(HexStr, i, 1)
If (ch >= "0") And (ch <= "9") Then
DecNum = DecNum + (Val(ch) * mult)
Else
If (ch >= "A") And (ch <= "F") Then
DecNum = DecNum + ((Asc(ch) - Asc("A") + 10) * mult)
Else
If (ch >= "a") And (ch <= "f") Then
DecNum = DecNum + ((Asc(ch) - Asc("a") + 10) * mult)
Else
HexToDec = 0
Exit Function
End If
End If
End If
mult = mult * 16
Next i
HexToDec = DecNum
End Function
Public Function DecToHex(ByVal DecNum As Double) As String
Dim remainder As Integer
Dim HexStr As String
HexStr = ""
Do While DecNum <> 0
remainder = DecNum Mod 16
If remainder <= 9 Then
HexStr = Chr(Asc(remainder)) & HexStr
Else
HexStr = Chr(Asc("A") + remainder - 10) & HexStr
End If
DecNum = DecNum \ 16
Loop
If HexStr = "" Then HexStr = "0"
DecToHex = HexStr
End Function
Public Function Convertir(ByVal numToConvert As Variant, ByVal DeBase As Integer, ByVal tobase As Integer) As Variant
Select Case DeBase
Case 10
If Not Verificar(Numero, "0123456789") Then
MsgBox "Decimales solo acepta del 0 al 9", vbExclamation
Exit Function
End If
Case 16
If Not Verificar(Numero, "0123456789ABCDEFabcdef") Then
MsgBox "La Base Hexadecimal sólo acepta del 0 al 9 y de las letras A a la F", vbExclamation
Exit Function
End If
End Select
If DeBase = tobase Then
Convertir = numToConvert
Exit Function
End If
Select Case DeBase
Case 10
Select Case tobase
Case 16
Convertir = DecToHex(numToConvert)
Exit Function
End Select
Case 16
Select Case tobase
Case 10
Convertir = HexToDec(numToConvert)
Exit Function
End Select
End Select
End Function
Private Function Verificar(valor As String, bag As String) As Boolean
Dim i As Integer
Verificar = True
For i = 1 To Len(valor)
If InStr(1, bag, Mid(valor, i, 1)) = 0 Then
Verificar = False
Exit Function
End If
Next i
End Function
en el formulario pone:
Código:
Private Sub Form_Load()
Numero = "22F2D4"
DeBase = 16
tobase = 10
MsgBox "El numero convertido es : " & Convertir(Numero, DeBase, tobase), vbInformation, "Bases Numeracion"
End Sub
ejecutalo y fijate que te devuelve cuanto vale ese 22F2D4 en decimal, el
codigo es entendible ,le decis un numero y despues a la base que lo queres convertir, 16 o 10...
ahora vamos a un ejemplo bien facil....vamos a sumar una A y una B de hexadecimal...la A vale 10 y la B vale 11...entonces tendria que dar 21 que en hexadecimal el 21 vale 15...el codigo seria algo asi:
Código:
Private Sub Form_Load()
Numero = "A"
DeBase = 16
tobase = 10
vNum1 = Convertir(Numero, DeBase, tobase)
Numero = "B"
DeBase = 16
tobase = 10
vNum2 = Convertir(Numero, DeBase, tobase)
Suma = vNum1 + vNum2
Numero = Suma
DeBase = 10
tobase = 16
Resultado = Convertir(Numero, DeBase, tobase)
MsgBox "La Suma es: " & Resultado, vbInformation, "Bases Numeracion"
End Sub
saludos.