gpt4 book ai didi

c++ - 如何在 C++ 中将 int 转换为 char*(string)?

转载 作者:太空宇宙 更新时间:2023-11-04 15:28:10 28 4
gpt4 key购买 nike

SOCKET lhSocket; 内部结果; lhSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); char *sendbuf = "这是一个测试"; iResult = send(lhSocket, sendbuf, (int)strlen(sendbuf), 0 );

printf("Bytes Sent: %ld\n", iResult);

我有使用 C++ 中的套接字的客户端和服务器程序,现在我发送一个缓冲区,当服务器确认我回来说我收到你的数据包时,它现在被服务器接收,我应该以字符串格式获取它,而不是接收到的字节:一些东西。如何实现?

我的 iresult 返回一个整数值,我正在通过套接字向服务器发送消息,我不想将其打印为发送的字节数:14。 我想打印作为字符串发送到服务器的消息。我正在处理套接字。我如何在 C++ 中实现这一目标

最佳答案

stringstream buf;
buf << 12345;
buf.str(); // string("12345")
buf.str().c_str(); // char* "12345"

关于c++ - 如何在 C++ 中将 int 转换为 char*(string)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3518929/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com