mario.deghetto

Visual Basic & .NET
posts - 319, comments - 204, trackbacks - 0

#18 Conversione da numero decimale a binario

Per convertire un numero intero in numero binario possiamo utilizzare la classe BitArray che converte un array di numeri in un array di bit. Per rappresentare la sequenza di bit, poi, utilizziamo una stringa e, per separare visivamente ogni byte, aggiungiamo anche uno spazio separatore:

Dim numero As Integer = 32767
Dim numDecimale() As Integer = {numero}
Dim numBinario As New BitArray(numDecimale)
Dim str As String = ""
Dim contaBit As Integer = 0
For i As Integer = (numBinario.Count - 1) To 0 Step -1
    If contaBit = 8 Then
        str &= " "
        contaBit = 0
    End If
    If numBinario.Item(i) = False Then
        str &= "0"
    Else
        str &= "1"
    End If
    contaBit += 1
Next
MessageBox.Show(str)

Con il numero intero 32767 otteniamo la stringa seguente:

00000000 00000000 01111111 11111111

Print | posted on domenica 25 ottobre 2009 5.28 |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 7 and type the answer here:

Powered by: