Octal To ASCII

This algorithm converts octal 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 OctalToDecimal(char* octal)
{
	int octLength = strlen(octal);
	double dec = 0;

	for (int i = 0; i < octLength; ++i)
	{
		dec += (octal[i] - 48) * pow(8, ((octLength - i) - 1));
	}

	return (int)dec;
}

char* OctalToASCII(char* oct) {
	char* ascii = "";
	int octLen = strlen(oct);

	for (int i = 0; i < octLen; i += 3)
	{
		ascii = AppendString(ascii, CharToString((char)OctalToDecimal(GetSubString(oct, i, 3))));
	}

	return ascii;
}
								


Example

									char* data = "120162157147162141155155151156147040101154147157162151164150155163";
char* value = OctalToASCII(data);
								


Output

									Programming Algorithms