1

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.

Sagar
  • 9,456
  • 6
  • 54
  • 96
  • Instead of naked arrays I suggest using `std::vector` or `std::array`. – Captain Obvlious Apr 28 '14 at 17:13
  • The answers are correct in that the printf is incorrect, but it leaves me wondering why you think that there is an issue sending data through a socket. I wouldn't expect the same type of error to occur when sending data through a socket so I'm confused. – shawn1874 Apr 28 '14 at 17:33

3 Answers3

2

The problem is that you're printing using printf("%X"). This converts the numeric value of each character to hexadecimal, so that the initial 5 becomes 35, and so on.

Use %c to print each character as a character. Alternatively, use the more type-safe std::cout to automatically do the "right" thing with characters.

Note that there's no need to copy the string into a new buffer. Just call mystring.data() or mystring.c_str() to get a pointer to the string's own character array.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

You're printing the two strings in two different ways. The first one you're printing as characters and the second as hex. Try printing the first the same way and I bet you'll get the same output:

#include <iostream>
#include <string>
#include <string.h>
#include <cstdio>

int main(int argc, char ** argv)
{
    std::string mystring = "5448495320574f4e5420574f524b";
    for (int i = 0; i < mystring.size(); i++)
    {
        printf("%X", mystring[i]);
    }
    printf("\n");
    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");
}
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
0

If I have understood you correctly then what you need is the following

#include <iostream>
#include <string>
#include <cctype>

int main()
{
    std::string s( "5448495320574f4e5420574f524b" );
    std::string::size_type n = s.size() & ( ~static_cast<std::string::size_type>( 1 ) );
    char *p = new char[n / 2];

    for ( std::string::size_type i = 0; i < n; i++ )
    {
        char c = std::isdigit( s[i] ) ? s[i] - '0' : std::toupper( s[i] ) - 'A' + 10;
        if ( i % 2 == 0 )
        {
            p[i / 2] = c;
        }
        else
        {
            p[i / 2] = ( ( unsigned char ) p[i / 2] << 4 ) | c;
        }
    }

    std::cout.write( p, n / 2 );
    std::cout << std::endl;

    delete []p;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335