我正在尝试制作一个非常简单的程序,将十进制转换为二进制,但我需要将它们颠倒过来

I am trying to make a very simple program that converts decimal to binaries but I need to write them reversed

class Decimal_to_binary
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine()); //input.

        while(n >= 1) // it stops if there is no number to divide.
        {
            int digit = n % 2; // this shows the digit .
            Console.Write(digit);
            n = n / 2; // for calculating another digit.
        }
        Console.WriteLine();
    }
}

输入:12
输出:0011(必须是 1100)

是自行车。

Convert.ToString(n, 2)

https://msdn.microsoft.com/en-us/library/14kwkz77(v=vs.110).aspx

如果你不想使用其他答案中提出的好方法,你可以这样做:

static void Main()
{
    int n = int.Parse(Console.ReadLine()); //input.

    // Use a string to store the values before reverting.
    string result = "";

    // If you have performance issues, use a StringBuilder instead of string.
    StringBuilder strBuilder = new StringBuilder();

    while (n >= 1) // it stops if there is no number to divide.
    {
        int digit = n % 2; // this shows the digit.
        result += digit; // string approach
        strBuilder.Append(digit); // StringBuilder approach
        n = n / 2; // for calculating another digit.
    }

    // Now you will have result == strBuilder.ToString()
    // from this point you can use which you prefer (I use result in this example)

    // OPTION 1: Use Reverse() extension method and then convert to a printable array.
    Console.WriteLine(result.Reverse().ToArray());

    // OPTION 2: Print the string backwards.
    for (int i = result.Length - 1; i >= 0; i--)
        Console.Write(result[i]);
}