0

I wanna make a c# application that convert a decimal number into binary with the input from the user. I get a red squiggly line on bin, when declaring bin = Convert.ToString(decToBin,2);. I don't understand my problem, so any help would be appreciated.

int decToBin;
Console.WrinteLine("Enter a number that will be converted to binary")
decToBin = Int32.Parse(Console.Readline());

bin = Convert.ToString(decToBin,2);

Console.ReadKey();
TNJ
  • 3
  • 3

4 Answers4

1

i guess you haven't declared bin. it should be

int decToBin;
Console.WriteLine("Enter a number that will be converted to binary");
decToBin = Int32.Parse(Console.ReadLine());
string bin = Convert.ToString(decToBin, 2);
Console.WriteLine(bin); 
Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • yeah you are right. Thank you for that, but I get another problem. When running this code, the outcome is stil a decimal number instead binary. What do I do wrong? Is my code correct for converting decimal into binary? – TNJ Mar 27 '14 at 12:02
  • @user3387813 you are getting decimal because you did not convert it into binary! This snippet simply accepts a number in "decToBin", converts it into string using "Convert.ToString()". But you are not converting it anywhere! – Bodhi Mar 27 '14 at 12:04
  • @Bodhi so ur saying that this line of code shoud be different? string bin = Convert.ToString(decToBin,2); – TNJ Mar 27 '14 at 12:13
  • @TNJ I am very sorry, i did not see that second parameter in Convert.ToString(). Can you tell me the inputs? – Bodhi Mar 27 '14 at 12:18
  • @TNJ what do you get in bin. isn't it 101? – Ehsan Mar 27 '14 at 12:20
  • @Bodhi Basicly I used this code as a converter: string binary = Convert.ToString(31, 2); the outcome will be 11111. I wanna make it dynamic instead hardcoded it. Thats why the first parameter is dynamic, the second is the base 2. Cauze binary is in base 2. – TNJ Mar 27 '14 at 12:33
  • @Ehsan what do you mean 101? If I run this app and my input is 13(example), the outcome will be 1101. – TNJ Mar 27 '14 at 12:40
  • @TNJ Yes, this is what i get if i enter 13. 1101. – Ehsan Mar 27 '14 at 12:47
  • @Ehsan I mean my outcome _italic_ should be 1101. But this code isn't working. I have changed it, but stil it doens't show what I would expected: 'code' string decToBin; 'code' Console.WriteLine("Enter a number to be converted into binary"); 'code' decToBin = Console.ReadLine(); 'code' decToBin = Convert.ToString(decToBin,2) 'code' Console.WrinteLine(decToBin); – TNJ Mar 27 '14 at 13:00
  • @TNJ you need to write this line at the end. Console.WriteLine(bin); – Ehsan Mar 27 '14 at 13:01
  • @Ehsan ahh YEss finaly it works. Thank you verry much;-) – TNJ Mar 27 '14 at 13:05
  • @Ehsan how do i suppose to do that, I'm a noob here. That was my first post of asking a question – TNJ Mar 28 '14 at 09:21
  • @TNJ see here http://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow – Ehsan Mar 28 '14 at 09:43
1

This works well and waits for key after print. you were also missing a comma and some large letters.

   Console.WriteLine("Enter a number that will be converted to binary");
   var decToBin = Int32.Parse(Console.ReadLine());

   Console.WriteLine(Convert.ToString(decToBin,2));

   Console.ReadKey();
Jammore
  • 11
  • 3
0

in addition to what Ehsan mentioned,

Console.Readline() should be Console.ReadLine(); L caps

Kapil
  • 9,469
  • 10
  • 40
  • 53
0

when declaring bin = Convert.ToString(decToBin,2);.

This is not how you declare variables in C#. The syntax is

<Type> <VariableName> [ = <Value>];

e.g.

string bin = Convert.ToString(decToBin, 2);

or

var bin = Convert.ToString(decToBin, 2);

Also see the C# language specification, §8.5.1:

The local-variable-type of a local-variable-declaration either directly specifies the type of the variables introduced by the declaration, or indicates with the identifier var that the type should be inferred based on an initializer.

helb
  • 7,609
  • 8
  • 36
  • 58