This is wrong:
string asciiNum = new char[len];
When you initialise a C++ string from a C-style character array, it assumes that the array is terminated - that is, that there's a zero-valued byte to mark the end of the string. This is not the case here - you allocate some memory (filled with garbage), attempt to initialise the string from it (with undefined results, since it might not find a terminator), and then leak the memory since you've lost the only pointer to it.
Instead, you want:
string asciiNum(len, '\0');
or, if you prefer, create it empty:
string asciiNum;
and use push_back
or +=
to append characters to it.
(This assumes that string
is the class from the standard library, even though you're not including <string>
or qualifying it with std::
. Perhaps your library is twenty years out of date?)