ASCII To Binary

This algorithm converts ASCII code to binary numbers.



									public static string ASCIIToBinary(string str)
{
	string bin = string.Empty;

	for (int i = 0; i < str.Length; ++i)
	{
		string cBin = DecimalToBinary(str[i]);

		if (cBin.Length < 8)
			cBin = cBin.PadLeft(8, '0');

		bin += cBin;
	}

	return bin;
}

private static string DecimalToBinary(int dec)
{
	if (dec < 1) return "0";

	string binStr = string.Empty;

	while (dec > 0)
	{
		binStr = binStr.Insert(0, (dec % 2).ToString());

		dec /= 2;
	}

	return binStr;
}
								


Example

									string data = "Programming Algorithms";
string value = ASCIIToBinary(data);
								


Output

									01010000011100100110111101100111011100100110000101101101011011010110100101101110011001110010000001000001011011000110011101101111011100100110100101110100011010000110110101110011