I want to convert a std::string
to hexadecimal in C++ using std::hex
manipulator but i can't get it working right.
This is what i wrote so far:
#include <iostream>
#include <sstream>
int main() {
std::string a = "hello world";
std::stringstream ss;
for(size_t i = 0; i < a.length(); i++) { // Print one character at the time
ss << std::hex << (int)a[i];
std::cout << ss.str() << " ";
ss.str(std::string());
}
return 0;
}
The output of the same string using hexdump is 6568 6c6c 206f 6f77 6c72 0a64
while
the previous code prints something like: 6865 6c6c 6f20 776f 726c 64
, so i'm suspecting this is something related with endianess of the platform. My question is: how can i convert a string without messing with endianess?