Faced the task of archiving files with the Huffman method, I work in binary mode and sometimes there is a terminating zero character in the files, which leads to problems with the use of string.

Question: Is it possible to somehow make the string consider it just a symbol? If so, how? And I really don’t want to suffer with Cish arrays of chars

1 answer 1

Transfer the length of the byte array to the string constructor:

 const char lit[] = "abc\0def"; std::string s(lit, sizeof(lit) - 1); // или std::string s(lit, lit + sizeof(lit) - 1); // но не // std::string s(lit); 

However, the string is not the most suitable type for an array of bytes, it is preferable to use std::vector<std::uint8_t> or gsl::span<std::uint8_t> .

  • your version is strange, considering that I read the data from the file, but thanks for the vector <uint8_t>, I think this is a great choice - Xambey
  • 2
    @Xambey, if you select a container for an array of work with bits, I would pay attention to the std bitset, it has all the operations for manipulating the bits and converting them (for this purpose, it was created), including the to_string method to convert it to string. - perfect
  • thanks again, the question is closed - Xambey