Copying non null-terminated unsigned char array to std::string

前端 未结 12 2203
别跟我提以往
别跟我提以往 2020-12-02 20:25

If the array was null-terminated this would be pretty straight forward:

unsigned char u_array[4] = { \'a\', \'s\', \'d\', \'\\0\' };
std::string str         


        
12条回答
  •  时光取名叫无心
    2020-12-02 20:41

    std::string has a constructor taking an array of char and a length.

    unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
    std::string str(reinterpret_cast(u_array), sizeo(u_array));
    

提交回复
热议问题