Friday 15 July 2011

c++ - how to convert each character in string to ascii value -



c++ - how to convert each character in string to ascii value -

i convert each character ascii separated x have 65x122x97x45......

#include <iostream> #include <string> int main(){ std::string text = "this text 123..."; homecoming 0; }

here solution using stringstreams

#include <iostream> #include <string> #include <sstream> int main(){ std::string text = "this text 123..."; std::istringstream sin(text); std::ostringstream sout; char temp; while(sin>>temp){ sout<<"x"<<std::hex<<(int)temp; } std::string output = sout.str(); std::cout<<output<<std::endl; homecoming 0; }

and output is:

x54x68x69x73x69x73x73x6fx6dx65x74x65x78x74x31x32x33x2ex2ex2e

c++

No comments:

Post a Comment