Your question, your audience. Choose who sees your identity—and your question—with question security.
#include <sstream>
#include <iomanip>
...
std::ostringstream oss;
for (int n = 0; n < (int)input_string; ++n)
{
oss << "0x" << std::hex << std::right << std::setw(2) << std::setfill('0')
<< (int)input_string[n] << ' ';
}
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
using namespace std;
template<class T> struct hexify {
hexify(stringstream& out) : ss(out) {}
void operator() (T& x) { ss << setbase(16) << "0x" << (int) x << ' ';}
stringstream& ss;
};
template<class T>
ostream& operator<<(ostream& os, const hexify<T>& h) { os << h.ss.str(); return os;}
int main() {
string s = "My error message";
stringstream ss;
cout << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;
// Or, as in your case:
//
// log4cpp::Category::getRoot() << log4cpp::Priority::DEBUG << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;
return 0;
}
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
using namespace std;
template<class T> struct hexify {
hexify(stringstream& out) : ss(out), count(0) {}
void operator() (T& x) { ss << setbase(16) << "0x" << (int) x << ' '; if(!((count +1 ) % 16)) ss << endl; ++count;}
stringstream& ss;
unsigned int count;
};
template<class T>
ostream& operator<<(ostream& os, const hexify<T>& h) { os << h.ss.str(); return os;}
int main() {
string s = "My error message My error message My error message My error message";
stringstream ss;
cout << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;
// Or, as in your case:
//
// log4cpp::Category::getRoot() << log4cpp::Priority::DEBUG << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;
return 0;
}
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
using namespace std;
template<class T> struct hexify {
hexify(stringstream& out) : ss(out), count(0) {}
void operator() (T& x) { ss << setbase(16) << "0x" << (int) x << ' '; if(!((count +1 ) % 16)) ss << endl; ++count;}
stringstream& ss;
unsigned int count;
};
template<class T>
ostream& operator<<(ostream& os, const hexify<T>& h) { os << h.ss.str(); return os;}
int main() {
string s = "My error message\n My error message\n My error message\n My error message";
stringstream ss;
cout << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;
// Or, as in your case:
//
// log4cpp::Category::getRoot() << log4cpp::Priority::DEBUG << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;
return 0;
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
From novice to tech pro — start learning today.
Open in new window
you could advance and improve the functor jkr has shown by
Open in new window
note, the code doesn't use template classes as it is specialized for char type only anyhow. it also doesn't require to pass a stringstream object but uses an own temporary.
Sara