jeffbRTC
jeffbRTC

Reputation: 2069

How to convert const uint8_t [] to std::string?

The below code yields an error,

    void emitData(const uint8_t data[], size_t size)
    {
        std::string encodedMessage(data, size);
    }

Error,

no instance of constructor "std::__2::basic_string<_CharT, _Traits, _Allocator>::basic_string [with _CharT=char, _Traits=std::__2::char_traits<char>, _Allocator=std::__2::allocator<char>]" matches the argument list -- argument types are: (const uint8_t *, size_t) 

Upvotes: 2

Views: 819

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118330

Use the overloaded constructor that takes two iterators as parameters: the beginning and ending iterator values. Simple pointers will do the trick:

std::string encodedMessage{data, data+size};

Upvotes: 4

Related Questions