Oppure

Loading
28/08/08 18:55
GrG
ciao!
stavolta non ho un problema...
Vb6 ha la funzione di convertire un numero in esadecimale con la funzione Hex(). Ma non è ha una per farlo tornare normale. Questo in varie occasioni mi ha seccato molto ma ho fatto una funzione Dec() che converte un esadecimale in decimale. Ve la posto in modo che se qualcuno ne avrà bisogno saprà che esiste un post in cui è scritta. La funzione la potete usare liberamente anche se preferirei che metteste un commento nel vostro progetto che mi citi.


Private Function Dec(ByVal Number As String) 'Created by GrG
Dim temp As String
Dim tmp As String
Dim i As Integer
Dim x As Integer
Dim c As Long
i = Len(CStr(Number))
temp = CStr(Number)
For x = 1 To i
tmp = Left(temp, 1)
temp = Right(temp, Len(temp) - 1)
If x = i Then
Dec = Dec & tmp
Else
Dec = Dec & tmp & "|"
End If
Next x
Dec = Replace(Dec, "A", "10")
Dec = Replace(Dec, "B", "11")
Dec = Replace(Dec, "C", "12")
Dec = Replace(Dec, "D", "13")
Dec = Replace(Dec, "E", "14")
Dec = Replace(Dec, "F", "15")
Dim d() As String
d = Split(Dec, "|")
Dim f As Integer
Dim m As Integer
m = UBound(d())
Dec = ""
For f = 0 To UBound(d())
c = c + (d(f) * (16 ^ m))
m = m - 1
Next f
Dec = c
End Function

Private Sub Form_Load() 'esempio sull'utilizzo
MsgBox Dec("7B")
End Sub


Per richiamare la funzione basta che scriviate:
Dec ("numero esadecimale";)

tra virgolette perchè all'inizio il numero viene trattato come una stringa...

altro esempio di utilizzo:
Private Sub Form_Load() 'esempio sull'utilizzo
Dim k
k = Hex(123) 'valore = 7B
MsgBox Dec(k) 'valore = 123
End Sub


p.s. Mi raccomando le lettere (A,B,C,D,E,F) del numero esadecimale DEVONO essere MAIUSCOLE, perchè il programma è CASESENSITIVE e darebbe errore.

Spero vi sia utile:)
Ultima modifica effettuata da GrG 28/08/08 18:58
aaa
29/08/08 8:05
antometal
si può anche con la funzione val:
val("&h" & hexNumber)

tipo...
decimale = Val("&h7e";)
decimale avrà il valore 126
aaa
29/08/08 9:16
GrG
LoL
Immaginavo che una funzione x convertire da esadecimale a decimale già fatta c'era...

vbb x lo meno ho imparat una cosa nuova, grazie ;)
aaa