You are on page 1of 2

'Funcion que convierte de numero Hexadecimal a Decimal

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
'Funcion que convierte de numero Decimal a Hexadecimal
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
Sub Macro1()
Dim num1 As String
Dim num2 As String
Dim numd1 As Integer
Dim numd2 As Integer
Dim resultado As String
Dim opcion As Integer
num1 = InputBox("Ingrese por favor el primer numero hexadecimal")
num2 = InputBox("Ingrese por favor el segundo numero hexadecimal")
numd1 = HexToDec(num1)
numd2 = HexToDec(num2)
opcion = InputBox("operaciones 1.suma 2.resta 3.multiplicacion 4.division ")
While opcion <> 5

Select Case opcion


Case 1
resultado = numd1 + numd2
Case 2
resultado = numd1 - numd2
Case 3
resultado = numd1 * numd2
Case 4
If numd2 = 0 Then
resultado = 9999
Else
resultado = numd1 / numd2
resultado = Int(resultado)
End If
End Select
'condision: para numeros negativos
If resultado < 0 Then
MsgBox ("No existen numeros hexadecimales negativos")
Else
If resultado = 9999 Then
'condicion: la division para cero no esta permitida
MsgBox ("La division para 0 no existe")
Else
'proceso para transforma de decimal a hexadecimal
resultado = DecToHex(resultado)
MsgBox ("El resultado de la operacion es: " & resultado)
End If
End If
opcion = InputBox("operaciones 1.suma 2.resta 3.multiplicacion 4.division ")
Wend
End Sub

You might also like