ASCII To Octal
This algorithm converts ASCII code to octal numbers.
/*****Please include following header files*****/
// string
/***********************************************/
/*****Please use following namespaces*****/
// std
/*****************************************/
static string DecimalToOctal(int dec) {
if (dec < 1) return "0";
string octStr = "";
while (dec > 0)
{
octStr = octStr.insert(0, to_string(dec % 8));
dec /= 8;
}
return octStr;
}
static string ASCIIToOctal(string str) {
string oct = "";
int strLen = str.length();
for (int i = 0; i < strLen; ++i)
{
string cOct = DecimalToOctal(str[i]);
int cOctLen = cOct.length();
if (cOctLen < 3)
for (size_t j = 0; j < (3 - cOctLen); j++)
cOct = cOct.insert(0, "0");
oct += cOct;
}
return oct;
}
Example
string data = "Programming Algorithms";
string value = ASCIIToOctal(data);
Output
120162157147162141155155151156147040101154147157162151164150155163