简体   繁体   English

如何将 const uint8_t [] 转换为 std::string?

[英]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) 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM