c++ - Convert Hex String to Hex Value -
i have large hex string
abcdef...
and want convert
0xab 0xcd 0xef
are there functions that?
also tell me means when people ask inputs in ascii or not? abcdef represented string. not sure if ascii or not. not sure mean. new programming here appreciated. have huge string need use in array , converting aforementioned format me initialize array hex string.
you can using string streams.
#include <iostream> #include <sstream> #include <vector> int main( int , char ** ) { const char *str = "afab2591cfb70e77c7c417d8c389507a5"; const char *p1 = str; const char *p2 = p1; std::vector<unsigned short> 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<unsigned short>::const_iterator = output.begin(); != output.end(); ++it ) { std::cout << std::hex << std::showbase << *it << "\t" << std::dec << std::noshowbase << *it << "\n"; } std::cout << "press key continue ..."; std::cin.get(); }
note if use unsigned char
instead of unsigned short
conversion stringstream attempts convert ascii character , doesn't work.
Comments
Post a Comment