c++如何向字符串添加零位字节或 null

发布于 2024-08-06 02:52:45 字数 1206 浏览 4 评论 0原文

我正在做一个简单的 TFTP 客户端实现。 在这里,我需要以以下格式发送读取请求,

/* send request 

      2 bytes    string     1 byte     string   1 byte
------------------------------------------------------
RRQ/  | 01/02 |  Filename  |   0  |    Mode    |   0  |
WRQ  -------------------------------------------------

*/

中间我必须插入 1 字节零位值。 但我无法添加该值。 另外,如果我添加一个 1 零位字节..这实际上意味着一个字符串终止字符 而不是如何获得正确的 strlen 值。

如果有人能帮我解决这个问题...

enter code here


const char opcode_read[2] ={'0','1'};
const char opcode_write[2] ={'0','2'};
const char opcode_data[2] ={'0','3'};
const char opcode_acknowledge[2] ={'0','4'};
const char opcode_error[2] ={'0','5'};  
const char mode_netascii[] = "netascii\0";
char blk_read_request[100];
char file_name[] = "rfc0791.txt\0";

memcpy(blk_read_request, opcode_read, 2);
memcpy(&blk_read_request[2], file_name, strlen(file_name) + 1);
memcpy(&blk_read_request[2 + strlen(file_name)], mode_netascii, strlen(mode_netascii) + 1);

for (int i = 0; i < strlen(blk_read_request); i++) {
    cout << i << " : " << blk_read_request[i] << " " << std::bitset<CHAR_BIT > (blk_read_request[i]) << "\n";
}

I am doing a simple client implementation of TFTP.
Here i need to send read request in following format

/* send request 

      2 bytes    string     1 byte     string   1 byte
------------------------------------------------------
RRQ/  | 01/02 |  Filename  |   0  |    Mode    |   0  |
WRQ  -------------------------------------------------

*/

in between i have to insert 1 byte zero bits value .
But i am not able to add that value.
Also if i add a 1 zero bits byte.. which actually means a string terminating character
than how to get proper strlen value.

If any one can help me with this...

enter code here


const char opcode_read[2] ={'0','1'};
const char opcode_write[2] ={'0','2'};
const char opcode_data[2] ={'0','3'};
const char opcode_acknowledge[2] ={'0','4'};
const char opcode_error[2] ={'0','5'};  
const char mode_netascii[] = "netascii\0";
char blk_read_request[100];
char file_name[] = "rfc0791.txt\0";

memcpy(blk_read_request, opcode_read, 2);
memcpy(&blk_read_request[2], file_name, strlen(file_name) + 1);
memcpy(&blk_read_request[2 + strlen(file_name)], mode_netascii, strlen(mode_netascii) + 1);

for (int i = 0; i < strlen(blk_read_request); i++) {
    cout << i << " : " << blk_read_request[i] << " " << std::bitset<CHAR_BIT > (blk_read_request[i]) << "\n";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟酒忠诚 2024-08-13 02:52:45

如果要传输 null,不仅必须使用“strlen(filename) + 1”进行 memcpy,而且还需要相应地更新后续 memcpy 的目标指针。

memcpy(&blk_read_request[2 + strlen(file_name) + 1], mode_netascii, strlen(mode_netascii) + 1);

注意其中额外的+1。

郑重声明,作为 C++,您可能需要考虑使用比“char *”更高级别的类,它可以处理嵌入的空字符。

例如,以下内容打印“8”,并打开 std::stringstream 作为形成数据包的更好方法:

#include <string>
#include <iostream>

int main()
{
    std::string x = "foo";
    x += '\0';
    x += "bar";
    x += '\0';

    std::cout << x.length() << std::endl;
}

If you want to transfer the null across, not only must you memcpy with "strlen(filename) + 1", but you'll also need to update the destination pointer for the subsequent memcpys accordingly.

memcpy(&blk_read_request[2 + strlen(file_name) + 1], mode_netascii, strlen(mode_netascii) + 1);

Note the extra +1 in there.

For the record, being C++, you might want want to consider using a higher-level class than "char *" which can handle embedded null characters.

For example, the following prints "8", and opens up std::stringstream as a better way of forming your packets:

#include <string>
#include <iostream>

int main()
{
    std::string x = "foo";
    x += '\0';
    x += "bar";
    x += '\0';

    std::cout << x.length() << std::endl;
}
独留℉清风醉 2024-08-13 02:52:45

这在 C++ 字符串中是自动的。

这:

const char mode_netascii[] = "netascii";

相当于:

const char mode_netascii[] = { 'n', 'e', 't', 'a', 's', 'c', 'i', 'i', '\0' };

This is automatic in C++ strings.

This:

const char mode_netascii[] = "netascii";

Is equivalent to this:

const char mode_netascii[] = { 'n', 'e', 't', 'a', 's', 'c', 'i', 'i', '\0' };
风筝在阴天搁浅。 2024-08-13 02:52:45

如果您需要处理将(或可能)包含空字节的数据,那么您通常会希望避免使用 C 样式字符串以及处理它们的函数。使用字符/字节数组并保留与缓冲区中数据长度相关的长度。

在 C++ 中,字节(或字符)向量对此非常有用。

同样在 C++ 中,std::string 类型可以包含空字符;但是,我建议避免使用它们并坚持使用 std::vector 因为它很容易陷入传递 string:: 的结果的一些错误陷阱c_str() 为期望以 null 结尾的字符串。

If you need to deal with data that will (or might) include null bytes then you'll generally want to avoid using C-style strings and the functions that deal with them. Use character/byte arrays and keep a length associated with the length of the data in the buffers.

In C++ vectors of bytes (or char) would work great for this.

Also in C++, the std::string type can contain null characters just fine; however, I'd suggest avoiding their use and sticking with std::vector<char> since it would be all too easy to fall into some bug traps of passing the result of string::c_str() to something that expects null terminated strings.

败给现实 2024-08-13 02:52:45
char *total_send_string = malloc(2+strlen(file_name)+sizeof(Mode)+3);
memset(total_send_string, 0, strlen(total_send_string);
//Then copy each member of the total send packet into the correct offset
//Now you have the correct result
char *total_send_string = malloc(2+strlen(file_name)+sizeof(Mode)+3);
memset(total_send_string, 0, strlen(total_send_string);
//Then copy each member of the total send packet into the correct offset
//Now you have the correct result
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文