ASCII To Binary

This algorithm converts ASCII code to binary numbers.



									/*****Please include following header files*****/
// string
/***********************************************/

/*****Please use following namespaces*****/
// std
/*****************************************/

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

	string binStr = "";

	while (dec > 0)
	{
		binStr = binStr.insert(0, string(1, (char)((dec % 2) + 48)));

		dec /= 2;
	}

	return binStr;
}

static string ASCIIToBinary(string str) {
	string bin = "";
	int strLength = str.length();

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

		if (cBinLength < 8) {
			for (size_t i = 0; i < (8 - cBinLength); i++)
				cBin = cBin.insert(0, "0");
		}

		bin += cBin;
	}

	return bin;
}
								


Example

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


Output

									01010000011100100110111101100111011100100110000101101101011011010110100101101110011001110010000001000001011011000110011101101111011100100110100101110100011010000110110101110011