const unsigned char * to std::string

LM. picture LM. · Apr 29, 2009 · Viewed 66.1k times · Source

sqlite3_column_text returns a const unsigned char*, how do I convert this to a std::string? I've tried std::string(), but I get an error.

Code:

temp_doc.uuid = std::string(sqlite3_column_text(this->stmts.read_documents, 0));

Error:

1>.\storage_manager.cpp(109) : error C2440: '<function-style-cast>' : cannot convert from 'const unsigned char *' to 'std::string'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous

Answer

Reunanen picture Reunanen · Apr 29, 2009

You could try:

temp_doc.uuid = std::string(reinterpret_cast<const char*>(
      sqlite3_column_text(this->stmts.read_documents, 0)
  ));

While std::string could have a constructor that takes const unsigned char*, apparently it does not.

Why not, then? You could have a look at this somewhat related question: Why do C++ streams use char instead of unsigned char?