如何将包含字节的字符串转换为c++中的字节数组?(复制)

时间:2022-09-01 12:05:09

This question already has an answer here:

这个问题已经有了答案:

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...".

我的字符串包含了“这不能工作”表示为十六进制。我想将字符串的内容复制到字符缓冲区中,这样当我发送544849时…在一个套接字上,它恰好在另一边收到,而不是“35343438…”。

I've tried using stringstream & std::hex as suggsted in other posts, but that does not work.

我试过使用stringstream & std::hex在其他帖子中被建议,但这是行不通的。

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().

对不起,这里的更多信息。如果它仍然是一个副本,我将关闭它。mystring中的数据就是一个例子。我实际得到的数据是在“内容”中通过AMQP发送的数据结构。getContent()返回std::string,就像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.

字符串的前两个字节为54。但是,当我将其写入套接字时,其他服务器将读取第一个字节为35,并使消息无效。

3 个解决方案

#1


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.

问题是您正在使用printf(“%X”)打印。这将每个字符的数值转换为十六进制,以便初始的5变成35,以此类推。

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.

使用%c打印每个字符作为字符。或者,使用更多类型安全的std::cout自动执行“正确”的字符。

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.

注意,不需要将字符串复制到新的缓冲区中。调用mystring.data()或mystring.c_str()来获取指向字符串自身字符数组的指针。

#2


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");
}

#3


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;
}

#1


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.

问题是您正在使用printf(“%X”)打印。这将每个字符的数值转换为十六进制,以便初始的5变成35,以此类推。

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.

使用%c打印每个字符作为字符。或者,使用更多类型安全的std::cout自动执行“正确”的字符。

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.

注意,不需要将字符串复制到新的缓冲区中。调用mystring.data()或mystring.c_str()来获取指向字符串自身字符数组的指针。

#2


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");
}

#3


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;
}