Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer to binary in C#

Tags:

c#

People also ask

How do you convert a number to binary?

To convert integer to binary, start with the integer in question and divide it by 2 keeping notice of the quotient and the remainder. Continue dividing the quotient by 2 until you get a quotient of zero. Then just write out the remainders in the reverse order.

Is binary function in C?

Binary literals don't exist in C. The closest you have are hexadecimal, as they follow the binary bitpattern closely. Hex to binary is really easy to convert.

How do you convert Bitwise to binary?

Logic to convert decimal to binary using bitwise operatorStore it in some variable say num. Declare an array of size required to store an integer in memory (i.e. 32 bits), say bin[INT_SIZE];. Initialize another variable to store index, say index = INT_SIZE - 1;. Run a loop from INT_SIZE to 0.


Your example has an integer expressed as a string. Let's say your integer was actually an integer, and you want to take the integer and convert it to a binary string.

int value = 8;
string binary = Convert.ToString(value, 2);

Which returns 1000.


Convert from any classic base to any base in C#

string number = "100";
int fromBase = 16;
int toBase = 10;

string result = Convert.ToString(Convert.ToInt32(number, fromBase), toBase);

// result == "256"

Supported bases are 2, 8, 10 and 16


Very Simple with no extra code, just input, conversion and output.

using System;

namespace _01.Decimal_to_Binary
{
    class DecimalToBinary
    {
        static void Main(string[] args)
        {
            Console.Write("Decimal: ");
            int decimalNumber = int.Parse(Console.ReadLine());

            int remainder;
            string result = string.Empty;
            while (decimalNumber > 0)
            {
                remainder = decimalNumber % 2;
                decimalNumber /= 2;
                result = remainder.ToString() + result;
            }
            Console.WriteLine("Binary:  {0}",result);
        }
    }
}

http://zamirsblog.blogspot.com/2011/10/convert-decimal-to-binary-in-c.html

    public string DecimalToBinary(string data)
    {
        string result = string.Empty;
        int rem = 0;
        try
        {
            if (!IsNumeric(data))
                error = "Invalid Value - This is not a numeric value";
            else
            {
                int num = int.Parse(data);
                while (num > 0)
                {
                    rem = num % 2;
                    num = num / 2;
                    result = rem.ToString() + result;
                }
            }
        }
        catch (Exception ex)
        {
            error = ex.Message;
        }
        return result;
    }

primitive way:

public string ToBinary(int n)
{
    if (n < 2) return n.ToString();

    var divisor = n / 2;
    var remainder = n % 2;

    return ToBinary(divisor) + remainder;
}

Convert.ToInt32(string, base) does not do base conversion into your base. It assumes that the string contains a valid number in the indicated base, and converts to base 10.

So you're getting an error because "8" is not a valid digit in base 2.

String str = "1111";
String Ans = Convert.ToInt32(str, 2).ToString();

Will show 15 (1111 base 2 = 15 base 10)

String str = "f000";
String Ans = Convert.ToInt32(str, 16).ToString();

Will show 61440.