c++如何向字符串添加零位字节或 null
我正在做一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果要传输 null,不仅必须使用“strlen(filename) + 1”进行 memcpy,而且还需要相应地更新后续 memcpy 的目标指针。
注意其中额外的+1。
郑重声明,作为 C++,您可能需要考虑使用比“char *”更高级别的类,它可以处理嵌入的空字符。
例如,以下内容打印“8”,并打开 std::stringstream 作为形成数据包的更好方法:
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.
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:
这在 C++ 字符串中是自动的。
这:
相当于:
This is automatic in C++ strings.
This:
Is equivalent to this:
如果您需要处理将(或可能)包含空字节的数据,那么您通常会希望避免使用 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 withstd::vector<char>
since it would be all too easy to fall into some bug traps of passing the result ofstring::c_str()
to something that expects null terminated strings.