| Function, Modul |
| |
| |
| Private Sub txtDezimal_Change() | |
| If IsNumeric(txtDezimal.Text) = False Then | Fehler abfangen. |
| txtDezimal.Text = "0" | |
| txtDezimal.SelStart = 0 | |
| txtDezimal.SelLength = 1 | |
| txtDezimal.SetFocus | |
| Exit Sub | |
| End If | |
| lblBinärzahl.Caption = umwandeln(txtDezimal.Text) | Funktion aufrufen. |
| End Sub | |
| |
| Private Sub txtDezimal_KeyPress(KeyAscii As Integer) | |
| Select Case Chr(KeyAscii) | Fehler abfangen / Tastatur zuweisen. |
| Case "0" To "9" | |
| Case Chr(vbKeyBack) | |
| |
| End Select | |
| End Sub | |
| |
| |
| Modulinhalt: | |
| Option Explicit | |
| |
| Function umwandeln(tmpZahl As Long) As String | Deklarationsteil. |
| Dim n As Long, anzahlStellen As Long, ergebnis$, bit As String * 1 |
| If tmpZahl = 0 Then | Wenn TextBox Null: Ergebnis = Null. |
| umwandeln = "0" | |
| Exit Function | |
| End If | |
| anzahlStellen = Log(tmpZahl) / Log(2) + 1 | Anzahl der Ziffern ermitteln. |
| For n = anzahlStellen - 1 To 0 Step -1 | |
| bit = tmpZahl \ 2 ^ n | |
| ergebnis = ergebnis & bit | |
| tmpZahl = tmpZahl - bit * 2 ^ n | |
| Next | |
| umwandeln = ergebnis | |
| End Function | |
| |