I'm trying to convert an std::string containing multiple values separated by spaces into a vector object. I have it working (meaning the debugger shows the correct values in the vector at the end of the algorithm), however I'm getting an exception that I've never seen before. The exception is below:
I've never seen this before, and I've been researching for an hour as to what is causing this and I still don't understand it.
My algorithm that's causing the exception is as follows:
vector<BYTE> ConvertStringOfValuesToVectorOfBytes(std::string valStr)
{
vector<string> vectorOfValues = split(valStr, ' '); //split string into vector of strings (works)
vector<BYTE> Hex;
vector<const char*> vectOfCStrings;
for(int i = 0; i < vectorOfValues.size(); i++)
{
const char* temp = vectorOfValues[i].c_str();
vectOfCStrings.push_back(temp);
}
//now we have a vector of c strings...
for(int i = 0; i < vectOfCStrings.size(); i++)
{
char temp = 0;
sscanf(vectOfCStrings[i], "%x", &temp);
Hex.push_back(temp);
}
return Hex;
} //<-- debugger gets to here and on the next step causes the exception
I have no clue as to how I can fix this problem. As I said, the algorithm "works" in the sense that I'm getting the right result, I just need to clear up the exception. How do I fix this?
What exactly is your algorithm trying to accomplish?
From the looks of it, you are breaking a std::string into a std::vector of 'words', and then converting those words into c-style strings (why?), then assembling a std:vector containing the first letter of each word interpreted as an int. Use a std:stringstream instead:
Generally, if your not directly interfacing with a C API, you shouldn't be using c-style strings in c++.