I have a large hex string
abcdef...
and I want to convert it to
0xab 0xcd 0xef
Are there any functions
You can do this using string streams.
#include
#include
#include
int main( int , char ** )
{
const char *str = "AFAB2591CFB70E77C7C417D8C389507A5";
const char *p1 = str;
const char *p2 = p1;
std::vector output;
while( *p2 != NULL ) {
unsigned short byte;
++p2;
if( *p2 != NULL ) {
++p2;
}
std::stringstream sstm( std::string( p1, p2 ) );
sstm.flags( std::ios_base::hex );
sstm >> byte;
output.push_back( byte );
p1 += 2;
}
for( std::vector::const_iterator it = output.begin(); it != output.end(); ++it ) {
std::cout << std::hex << std::showbase << *it << "\t" << std::dec << std::noshowbase << *it << "\n";
}
std::cout << "Press any key to continue ...";
std::cin.get();
}
Note that if you use unsigned char instead of unsigned short the conversion from stringstream attempts to convert it into an ASCII character and it doesn't work.