Hola. Estoy desesperado con un pequeño problema.
Tengo un fichero binario con números hexadecimales y al abrirlo para leerlo y convertir los valores hexadecimales a números decimales no me lo hace.
Por ejemplo, al querer leer la posición 2 del fichero (con valor 14h), trato de almacenar ese dato en un string (cadena) para luego convertirlo a decimal y no lo mete en cadena.
Este es el código:
Private Sub Command1_Click()<br style="font-style: italic;">Dim cadena As String<br style="font-style: italic;">Open App.Path & "prueba.ov2" For Binary As #1<br style="font-style: italic;">Get #1, 2, cadena<br style="font-style: italic;">Text1.Text = cadena<br style="font-style: italic;">Text2.Text = ConvertHexToDecimal(cadena)<br style="font-style: italic;">Close<br style="font-style: italic;">End Sub
El módulo con el algoritmo ConvertHexToDecimal(cadena) es:
Const HexA = 10<br style="font-style: italic;">Const HexB = 11<br style="font-style: italic;">Const HexC = 12<br style="font-style: italic;">Const HexD = 13<br style="font-style: italic;">Const HexE = 14<br style="font-style: italic;">Const HexF = 15<br style="font-style: italic;"><br style="font-style: italic;">Public Function ConvertHexToDecimal(ByVal HexValue As String) As Variant<br style="font-style: italic;"> Dim rValue, A As Long<br style="font-style: italic;"> Dim Temp, Rev As String<br style="font-style: italic;"> <br style="font-style: italic;"> Rev = StrReverse(HexValue)<br style="font-style: italic;"> <br style="font-style: italic;"> For A = 1 To Len(HexValue)<br style="font-style: italic;"> Temp = Mid$(Rev, A, 1)<br style="font-style: italic;"> <br style="font-style: italic;"> If Val(Temp) = Temp Then<br style="font-style: italic;"> 'Character is a number<br style="font-style: italic;"> If A = 1 Then<br style="font-style: italic;"> <br style="font-style: italic;"> Else<br style="font-style: italic;"> rValue = rValue + (Val(Temp) * (16 ^ (A - 1)))<br style="font-style: italic;"> <br style="font-style: italic;"> End If<br style="font-style: italic;"> Else<br style="font-style: italic;"> Select Case LCase$(Temp)<br style="font-style: italic;"> <br style="font-style: italic;"> Case "a"<br style="font-style: italic;"> If A = 1 Then<br style="font-style: italic;"> rValue = HexA<br style="font-style: italic;"> Else<br style="font-style: italic;"> rValue = rValue + (HexA * (16 ^ (A - 1)))<br style="font-style: italic;"> End If<br style="font-style: italic;"> Case "b"<br style="font-style: italic;"> If A = 1 Then<br style="font-style: italic;"> rValue = HexB<br style="font-style: italic;"> Else<br style="font-style: italic;"> rValue = rValue + (HexB * (16 ^ (A - 1)))<br style="font-style: italic;"> End If<br style="font-style: italic;"> Case "c"<br style="font-style: italic;"> If A = 1 Then<br style="font-style: italic;"> rValue = HexC<br style="font-style: italic;"> Else<br style="font-style: italic;"> rValue = rValue + (HexC * (16 ^ (A - 1)))<br style="font-style: italic;"> End If<br style="font-style: italic;"> Case "d"<br style="font-style: italic;"> If A = 1 Then<br style="font-style: italic;"> rValue = HexD<br style="font-style: italic;"> Else<br style="font-style: italic;"> rValue = rValue + (HexD * (16 ^ (A - 1)))<br style="font-style: italic;"> End If<br style="font-style: italic;"> Case "e"<br style="font-style: italic;"> If A = 1 Then<br style="font-style: italic;"> rValue = HexE<br style="font-style: italic;"> Else<br style="font-style: italic;"> rValue = rValue + (HexE * (16 ^ (A - 1)))<br style="font-style: italic;"> End If<br style="font-style: italic;"> Case "f"<br style="font-style: italic;"> If A = 1 Then<br style="font-style: italic;"> rValue = HexF<br style="font-style: italic;"> Else<br style="font-style: italic;"> rValue = rValue + (HexF * (16 ^ (A - 1)))<br style="font-style: italic;"> End If<br style="font-style: italic;"> End Select<br style="font-style: italic;"> End If<br style="font-style: italic;"> DoEvents<br style="font-style: italic;"> <br style="font-style: italic;"> Next A<br style="font-style: italic;"> ConvertHexToDecimal = rValue<br style="font-style: italic;"><br style="font-style: italic;">End Function
Y el caso es que saqué el algoritmo de un proyecto en el que se metía un valor hexadecimal en un textbox y funcionaba.
Gracias