-2

I'm learning C# and im trying to get a custom number to convert to binary. I've looked up several forms here on stack overflow and they have similar code to this, but it doesn't work. Any ideas?

Console.WriteLine("You have chosen binary, input a number then it will be converted to binary.");
string num1input = Console.ReadLine();
double num1 = double.Parse(num1input);
var binary = Convert.ToString(num1, 2);
Console.WriteLine("{0} converted to binary is {1} " ,num1, binary);
Daxtron2
  • 1,239
  • 1
  • 11
  • 19
  • 2
    What do you mean by "to binary"? Do you mean a `string` representation containing the base-2 representation of an `Int32` value - or something else? – Dai May 22 '18 at 16:10
  • What's a custom number? It looks like you're expecting the number to be entered as a floating point value. Do you want to generate the binary equivalent of a floating point number? – Sudheesh Singanamalla May 22 '18 at 16:10
  • 3
    I note that your example uses `System.Double` which is an IEEE-754 floating-point type, which has a non-trivial binary representation. – Dai May 22 '18 at 16:10
  • What do you mean by `but it doesn't work`? – Chetan May 22 '18 at 16:11
  • None of this code has anything to do with conversion to binary. I think you misunderstand what a `double` type is. As Dai said, it's a floating point number. – Daxtron2 May 22 '18 at 16:14
  • 3
    Possible duplicate of [Convert integer to binary in C#](https://stackoverflow.com/questions/2954962/convert-integer-to-binary-in-c-sharp) – SomeGuy May 22 '18 at 16:18
  • There isn't an overload of `Convert.ToString` that takes a double and a base. The compiler will be complaining that it can't convert from int to IFormatProvider.. – stuartd May 22 '18 at 16:20
  • you are trying to use Convert.ToString Method (Int32, Int32) with double – MikeT May 22 '18 at 16:26

2 Answers2

2

It's not clear from your question if you are trying to convert an integer or a floating point value to a binary string representation.

For an integer, use this code:

void Main()
{
    Console.WriteLine("You have chosen binary, input a number then it will be converted to binary.");
    string num1input = Console.ReadLine();
    int num1 = int.Parse(num1input);

    var binary = Convert.ToString(num1, 2);

    Console.WriteLine("{0} converted to binary is {1} ", num1, binary);
}

For a double, this code might be what you want:

void Main()
{
    Console.WriteLine("You have chosen binary, input a number then it will be converted to binary.");
    string num1input = Console.ReadLine();
    double num1 = double.Parse(num1input);

    long bits = BitConverter.DoubleToInt64Bits(num1);

    var binary = Convert.ToString(bits, 2);

    Console.WriteLine("{0} converted to binary is {1} ", num1, binary);
}
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
0

I found this exercise interesting, so here is what I did :

public IEnumerable<char> ConvertToBase2(int myNumber) {
    while(myNumber != 0) {
        var returnValue = (myNumber%2 == 0) ? '0' : '1';

        myNumber = (myNumber % 2 == 0) ? myNumber / 2 : 
            (myNumber - 1) / 2;

        yield return returnValue;
    }
}
Console.Write(String.Concat(
    ConvertToBase2(9).Reverse()
));

It works only for integers. I don't remember exactly how floating points numbers are implemented.

By the way, this is a better answer ;-)

Deblaton Jean-Philippe
  • 11,188
  • 3
  • 49
  • 66