I want to make a converter for decimal to binary using a function but I don't see what is wrong with my code: (the errors states that i can not simplicity convert string to char but I have converted it in line 12). Edit - I am interested in the process of converting denary to binary rather than the output.
static void Main (string[] args)
{
Console.WriteLine("Decimal 14 = Binary " + dec_to_bin(14));
Console.WriteLine("Decimal 100 = Binary " + dec_to_bin(100));
Console.WriteLine("Decimal 32 = Binary " + dec_to_bin(32));
Console.WriteLine("Decimal 64 = Binary " + dec_to_bin(64));
Console.ReadLine();
}
public static string dec_to_bin(int dec)
{
string binary = "11111111";
char[]binaryArray = binary.ToCharArray();
for (int i = 0; i < 8; i++)
{
if (dec % 2 == 0)
{
binaryArray[i] = "0"; // error 1
}
else
{
binaryArray[i] = "1"; // error 2
}
}
binary = new string(binaryArray);
return binary;
}