Sample code:
#include <iostream>
#include <string>
#include <string.h>
#include <cstdio>
int main(int argc, char ** argv)
{
std::string mystring = "5448495320574f4e5420574f524b";
std::cout << mystring << std::endl;
char buffer[mystring.size()];
memset(buffer, '\0', mystring.size());
mystring.copy(buffer, mystring.size(), 0);
for (int i = 0; i < mystring.size(); i++)
{
printf("%X", buffer[i]);
}
printf("\n");
}
Output:
5448495320574f4e5420574f524b
35343438343935333230353734663465353432303537346635323462
Question:
My string contains "THIS WONT WORK" represented as hex. I'd like to copy the content of the string as hex into a character buffer, such that when I send 544849...
over a socket, it receives exactly that on the other side, and not "35343438...".
I've tried using stringstream
& std::hex
as suggsted in other posts, but that does not work.
EDIT
Sorry, more information here. If it's still a duplicate, I'll close it. The data in mystring was an example. The data I am actually getting is a data structure sent over AMQP in the "content". The getContent() returns a std::string, just like the getContentBytes().
The first two bytes of the the string are 54. However, when I write that to a socket, the other server is reading the first bytes as 35, and invalidating the message.