Convertire un numero da rappresentazione binaria in decimale

Da Gambas-it.org - Wikipedia.
Versione del 13 dic 2012 alle 09:46 di Vuott (Discussione | contributi) (Nuova pagina: Per convertire un numero, espresso in formato a rappresentazione binaria, nella corrispondente rappresentazione ''decimale'', potremo utilizzare questo algoritmo: |1| '''Pu...)

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

Per convertire un numero, espresso in formato a rappresentazione binaria, nella corrispondente rappresentazione decimale, potremo utilizzare questo algoritmo: |1|

Public Sub Button1_Click()  

Dim b, e As Integer  
Dim a As String  
Dim Intero, InteroFinale As Integer  
Dim Esponente, crt As Integer

 a = InputBox("Immetti un numero in formato binario:") 
 
 Esponente = 0  
   
  For b = Len(a) To 1 Step -1  
  
    crt = Int(Val(Mid(a, b, 1)))  
    Intero = crt * 2 ^ Esponente  
    Esponente += 1  
    InteroFinale = InteroFinale + Intero  
   
  Next  
 
 Print InteroFinale

End

oppure quest'altro:

Private c[32] As Integer


Public Sub Button1_Click()

 Dim b, e As Integer  
 Dim a As String  
 Dim d As Integer = 1
  
  a = InputBox("Immetti un numero binario:")  
 
  If a = Null Then Return  
 
  For b = Len(a) To 1 Step -1  
 
    If b = Len(a) Then  
      Select Case Right$(a, 1)  
        Case 1  
          c[b] = 1  
          d = 1  
        Case 0  
          c[b] = 0  
      End Select  
    
    Else  
      d = d * 2  
      If Mid$(a, b, 1) = 1 Then  
        c[b] = d  
      Else  
        c[b] = 0  
      Endif  
    Endif  
 
 
    e = e + c[b]  
 
  Next  
 
  Print "Corrisponde al numero:"  
  Print " - decimale: " & e  
  Print " - esadecimale: "; Hex$(Str(e))

End



Note:

[1] Il presente algoritmo è stato suggerito dall'utente Picavbg del forum di Gambas-it.org.