Ecco come fare conversioni da Dec > Bin > Dec > Hex e calcoli di radianti e gradi.

Nel programma basta usare:

1
2
3
4
5
6
7
8
9
10
Bin(Num_decimale)
BinToDec(Num_bin)
Hex(Num_decimale)
'Hex questo non è presente nel modulo, ma è già integrato in vb6.

Radians(valore_gradi)
Degrees(valore_radiante)

'Ovviamente tutti letti da una variabile o un campo..
variabile = Bin(34)

Crea un Modulo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Option Explicit

'  Conversione da bdecimale a binario
Function Bin(ByVal value As Long) As String
    Dim result As String, exponent As Integer
    result = String$(32, "0")
    Do
        If value And Power2(exponent) Then
            Mid$(result, 32 - exponent, 1) = "1"
            value = value Xor Power2(exponent)
        End If
        exponent = exponent + 1
    Loop While value
    Bin = Mid$(result, 33 - exponent)
End Function

' Convesrione da binario a decimale.
Function BinToDec(value As String) As Long
    Dim result As Long, i As Integer, exponent As Integer
    For i = Len(value) To 1 Step -1
        Select Case Asc(Mid$(value, i, 1))
            Case 48      ' "0", niente.
           Case 49      ' "1", aggiungere la corrispondente potenza di 2
               result = result + Power2(exponent)
            Case Else
                Err.Raise 5
        End Select
        exponent = exponent + 1
    Next
    BinToDec = result
End Function

' conversione gradi > radiante.
Function Radians(Degrees As Double) As Double
    Radians = Degrees / 57.29577951
End Function

' Convertire radiante in gradi.
Function Degrees(Radians As Double) As Double
    Degrees = Radians * 57.29577951
End Function

'Private function
Private Function Power2(ByVal exponent As Long) As Long
    Static result(0 To 31) As Long, i As Integer
    If result(0) = 0 Then
        result(0) = 1
        For i = 1 To 30
            result(i) = result(i - 1) * 2
        Next
        result(31) = &H80000000
    End If
    Power2 = result(exponent)
End Function

Tags: , , , , ,

Articoli vicini: Prissimo articolo [VB6] Calcolare l’anno bisestile
Articolo precedente Catturare lo schermo senza il tasto Stamp