-1

I'm trying to write an app to convert different number systems. In the code below I check if the rest is "0" or "1". If it is the program should add "0" or "1" to my result string binaer. It don't work and I have really no idea why.

public void dezinbin(int dez) {
    var binaer = "";        // erstellt den leeren Ausgabestrang
    var platzhalter = dez;                  //Platzhalter für dez, da des nicht verändert werden kann

    for (var i = 0; i<16; i++){
        var binarrest = platzhalter % 2;  //teilt dez durch 2 und speichert den rest in binaerrest
        platzhalter = platzhalter / 2;          //verringert die dez-Eingabe um die hälfte
        if (binarrest == 0) //hängt den binaerrest jeweils an den Anfang des Ausgabestrings
        {
            binaer.Insert(0, "0");
        }
        else 
        {
            binaer.Insert(0, "1");
        }
                    }
    this.bin = binaer;   //gibt den Ausgabestring zurück
}
Servy
  • 202,030
  • 26
  • 332
  • 449
Fabian
  • 23
  • 2
  • 2
    Please define "doesn't work". Does it crash? Do you get an error? Does it run but give the wrong result? – Jason Sep 08 '16 at 15:23
  • further, C# has built in functions to handle this conversion: http://stackoverflow.com/questions/2954962/decimal-to-binary-conversion-in-c – Jason Sep 08 '16 at 15:24

3 Answers3

1

Am I right to understand you want to append "0" or "1" to the string binaer?

if you want to append to the end try

    binaer +=  "1"

if you want to put it in front , then try

    binaer = "1" + binaer;
anil
  • 598
  • 1
  • 6
  • 20
1

please use this syntax:

binaer = binaer.Insert(0, "0");

because insert has not changed the "binaer" itself.

henry
  • 41
  • 2
0

try using binaer += "0"; instead of binaer.Insert(0, "0");

and so on and so forth.

Farhan Ahmed
  • 342
  • 1
  • 8