Visual Basic Foro
Programación => Visual Basic 6 => Mensaje iniciado por: Bazooka en Noviembre 16, 2012, 10:25:44 am
-
HOLA AMIGOS ESTOY UTILIZANDO ESTA FUNCION QUE PEGO ABAJO Y AL PARECER ME DEVUELVE MAL LOS VALORES EN ALGUNAS CIRCUNSTANCIAS.
NOTEN QUE EN EL EJEMPLO DEL Command1_Click LLAMO A LA FUNCION 4 VECES Y LOS 3 PRIMEROS VALORES ME DA UN VALOR HEXA CORRECTO PERO LA 4º ME DEVUELVE 1000 Y DUDO DE QUE SEA CORRECTO .
QUE LES PARECE??
Private Sub Command1_Click()
'LLAMANDO ASI DEVUELVE
Text1 = BinToHex("111111100010101111111") '= 1FC57F
Text1 = BinToHex("100000101010101000001") '= 105541
Text1 = BinToHex("101110101011001011101") '= 17565D
Text1 = BinToHex("000000001000000000000") '= 1000
End Sub
Function BinToHex(BinNum As String) As String
Dim BinLen As Integer, I As Integer
Dim HexNum As Variant
On Error GoTo ErrorHandler
BinLen = Len(BinNum)
For I = BinLen To 1 Step -1
' Check the string for invalid characters
If Asc(Mid(BinNum, I, 1)) < 48 Or _
Asc(Mid(BinNum, I, 1)) > 49 Then
HexNum = ""
Err.Raise 1002, "BinToHex", "Invalid Input"
End If
' Calculate HEX value of BinNum
If Mid(BinNum, I, 1) And 1 Then
HexNum = HexNum + 2 ^ Abs(I - BinLen)
End If
Next I
' Return HexNum as String
BinToHex = Hex(HexNum)
ErrorHandler:
End Function
-
esta bien...
-
Estimado Bazooka
El resultado es correcto, si quieres salir de duda en la calculadora de Windows cámbiala a la modalidad Científica, luego selecciona Bin pegas o anotas el valor en Binario y finalmente selecciona Hex y podrás ver el resultado de la transformación a Hexadecimal.
Saludos, desde algún lugar de Lima-Perú
-
Ok gracias amigos!!!