const unsigned char * to std::string


Question

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
1
39
4/29/2009 8:28:18 PM

Accepted Answer

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?

49
5/23/2017 11:54:34 AM

On the off-chance you actually want a string of unsigned characters, you could create your own type:

typedef std::basic_string <unsigned char> ustring;

You should then be able to say things like:

ustring s = sqlite3_column_text(this->stmts.read_documents, 0);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon