Binary To ASCII

This algorithm converts binary numbers to ASCII code.



									/*****Please include following header files*****/
// math.h
/***********************************************/

char* GetSubString(char* str, int index, int count) {
	int strLen = strlen(str);
	int lastIndex = index + count;

	if (index >= 0 && lastIndex > strLen) return "";

	char* subStr = malloc(count + 1);

	for (int i = 0; i < count; i++) {
		subStr[i] = str[index + i];
	}

	subStr[count] = '\0';

	return subStr;
}

char* AppendString(const char* str1, const char* str2) {
	int str1Len = strlen(str1);
	int str2Len = strlen(str2);
	int strLen = str1Len + str2Len + 1;
	char* str = malloc(strLen);

	for (int i = 0; i < str1Len; i++)
		str[i] = str1[i];

	for (int i = 0; i < str2Len; i++)
		str[(str1Len + i)] = str2[i];

	str[strLen - 1] = '\0';

	return str;
}

char* CharToString(char c) {
	char* str = malloc(2);
	str[0] = c;
	str[1] = '\0';

	return str;
}

int BinaryToDecimal(char* bin)
{
	int binLength = strlen(bin);
	double dec = 0;

	for (int i = 0; i < binLength; ++i)
	{
		dec += (bin[i] - 48) * pow(2, ((binLength - i) - 1));
	}

	return (int)dec;
}

char* BinaryToASCII(char* bin) {
	char* ascii = "";
	int binLen = strlen(bin);

	for (int i = 0; i < binLen; i += 8)
	{
		ascii = AppendString(ascii, CharToString((char)BinaryToDecimal(GetSubString(bin, i, 8))));
	}

	return ascii;
}
								


Example

									char* data = "01010000011100100110111101100111011100100110000101101101011011010110100101101110011001110010000001000001011011000110011101101111011100100110100101110100011010000110110101110011";
char* value = BinaryToASCII(data);
								


Output

									Programming Algorithms