Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C+中将包含字节的字符串转换为字节数组+;?_C++_C_Hex_Byte - Fatal编程技术网

C++ 如何在C+中将包含字节的字符串转换为字节数组+;?

C++ 如何在C+中将包含字节的字符串转换为字节数组+;?,c++,c,hex,byte,C++,C,Hex,Byte,示例代码: #include <iostream> #include <string> #include <string.h> #include <cstdio> int main(int argc, char ** argv) { std::string mystring = "5448495320574f4e5420574f524b"; std::cout << mystring << std::end

示例代码:

#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");
}
问题:

我的字符串包含用十六进制表示的“这不起作用”。我想将字符串的内容复制为十六进制到一个字符缓冲区中,这样当我通过套接字发送
544849…
时,它会在另一侧接收到该内容,而不是“353438…”

我尝试过使用其他帖子中建议的
stringstream
&
std::hex
,但这不起作用

编辑

对不起,这里有更多信息。如果它还是复制品,我就把它关了。 mystring中的数据就是一个例子。我实际得到的数据是通过“内容”中的AMQP发送的数据结构。getContent()返回一个std::字符串,就像getContentBytes()一样


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

您正在以两种不同的方式打印这两个字符串。第一个打印为字符,第二个打印为十六进制。尝试以相同的方式打印第一个,我打赌您将获得相同的输出:

#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");
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
std::string mystring=“5448495320574f4e5420574f524b”;
对于(int i=0;i
问题是您正在使用
printf(“%X”)
进行打印。这会将每个字符的数值转换为十六进制,从而使初始的
5
变为
35
,依此类推

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


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

如果我理解正确,则需要以下内容

#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;
}
#包括
#包括
#包括
int main()
{
标准::字符串s(“5448495320574f4e5420574f524b”);
std::string::size_type n=s.size()&(~static_cast(1));
char*p=新字符[n/2];
对于(std::string::size_type i=0;ip[i/2]=((无符号字符)p[i/2]我建议使用
std::vector
std::array
而不是裸数组。答案是正确的,printf是不正确的,但这让我想知道,为什么您认为通过套接字发送数据会有问题。我不希望通过套接字发送数据时出现相同类型的错误,所以我感到困惑。
#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;
}