c++ how to add a zero bits byte or null to a string

10,570

Solution 1

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

Solution 2

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.

Solution 3

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' };
Share:
10,570
Admin
Author by

Admin

Updated on June 28, 2022

Comments

  • Admin
    Admin almost 2 years

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