You can either convert the entire string into a larger integral type, and pick out the bytes from that. Something like:
std::vector<unsigned char> asBytes( std::string const& input ) { std::istringstream parser( input ); uint32_t tmp; input >> std::hex >> tmp; std::vector<unsigned char> results; // The conversion implicitly does the & 0xFF results.push_back( tmp >> 24 ); results.push_back( tmp >> 16 ); results.push_back( tmp >> 8 ); results.push_back( tmp ); return results; }
Or, you could create substrings of two characters each, create an std::istringstream for each, and input from it. You'd still have to input to a type larger than char, because >> to a character type reads one character only, and assigns it. But you can read it into an int, then convert the int to unsigned char.
unsigned char convertOneByte( std::string::const_iterator begin, std::string::const_iterator end ) { std::istringstream parser( std::string( begin, end ) ); int tmp; parser >> std::hex >> tmp; return tmp; } std::vector<unsigned char> asBytes( std::string const& input ) { std::vector<unsigned char> results; results.push_back( input.begin() , input.begin() + 2 ); results.push_back( input.begin() + 2, input.begin() + 4 ); results.push_back( input.begin() + 4, input.begin() + 6 ); results.push_back( input.begin() + 6, input.begin() + 8 ); return results; }
(Both bits of code need a lot more error checking. They're just to give you an idea.)
unsigned charthe value of each pair of chars?