Autor Beitrag
ThomAlex
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 58

Windows 7
C# (VS08) Games via XNA (privat) oder WinForms (Schule)
BeitragVerfasst: Sa 14.11.09 18:35 
Hallo ich möchte eine Dezimalzahl eingeben und die Binärzahl davon ausgegeben haben:

Bsp: 12(dezimal) = 1100(binär)

Mein Programm gibt es genau falsch rum aus (0011), ich weiß auch warum, aber weiß nicht wie ich es anders rum hinbekomme, wäre sehr nett, wenn Ihr mir helfen könntet

ausblenden C#-Quelltext
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:
private void btnDezToBin_Click(object sender, EventArgs e)
        {
            int zahl = int.Parse(txtEingabe.Text);
            int zahl2 = zahl / 2;
            int[] array = new int[zahl2 + 1];
            string text = "";
            int a = 0;
            for (int i = zahl; i > 0; i = i / 2)
            {
                
                if (zahl % 2 == 0)
                {
                    array[a] = 0;
                    text += array[a].ToString();
                }
                if (zahl % 2 == 1)
                {
                    array[a] = 1;
                    text += array[a].ToString();
                }
                a++;
                zahl = zahl / 2;
            }
        }
            lblAusgabe.Visible = true;
            lblAusgabe.Text = text;


Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Moderiert von user profile iconChristian S.: Topic aus WinForms verschoben am Sa 14.11.2009 um 17:39
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Sa 14.11.09 18:40 
ausblenden C#-Quelltext
1:
text += array[a].ToString();					

Löse mal das "+=" auf (ist ja nur eine Kurzform) und überlege dann noch einmal ganz scharf, wie du die aktuelle Ziffer nicht hinten anhängst.

_________________
>λ=
ThomAlex Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 58

Windows 7
C# (VS08) Games via XNA (privat) oder WinForms (Schule)
BeitragVerfasst: Sa 14.11.09 18:45 
Vielen Dank, dummer Fehler von mir:

Lösung

Anstatt: text += array[a].ToString();

Besser: text = array[a].ToString() + text;

Ich sollte nächstes mal den Code genauer analysieren, aber danke =) MFG