58

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

9 Answers 9

75

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?

5
  • 2
    +1. although, I think you should make that "const char *" inside your angle brackets. You don't want to be trying to cast away the const-ness (which it may not even allow).
    – rmeador
    Apr 29, 2009 at 20:33
  • I get this error: 1>.\storage_manager.cpp(109) : error C2440: 'static_cast' : cannot convert from 'const unsigned char *' to 'const char *' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    – LM.
    Apr 29, 2009 at 20:35
  • Uh-oh. Well, maybe you need reinterpret_cast instead of static_cast, then?
    – Reunanen
    Apr 29, 2009 at 20:38
  • Since I'm quite sure that it will work, I'll edit the answer too.
    – Reunanen
    Apr 29, 2009 at 20:39
  • You mean reinterpret_cast instead of static_cast? See: stackoverflow.com/questions/573294/when-to-use-reinterpretcast
    – Reunanen
    Apr 29, 2009 at 20:44
21

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);
0
20

The reason people typically use an (unsigned char *) type is to indicate that the data is binary and not plain ASCII text. I know libxml does this, and from the looks of it, sqlite is doing the same thing.

The data you're getting back from the sqlite call is probably UTF-8 encoded Unicode text. While a reinterpret_cast may appear to work, if someone ever stores text in the field that is not plain ASCII, your program probably won't be well-behaved.

The std::string class isn't designed with Unicode in mind, so if you ask for the length() of a string, you'll get the number of bytes, which, in UTF-8, is not necessarily the same thing as the number of characters.

Short answer: the simple cast may work, if you're certain the data is just ASCII. If it can be any UTF-8 data, then you need to handle encoding/decoding in a smarter way.

2
  • 1
    Is there a way of doing this with the std:: library?
    – LM.
    May 9, 2009 at 21:10
  • Not that I know of. The standard approach is to use a 3rd party library, for example: site.icu-project.org May 12, 2009 at 22:58
4

I'm not familiar with sqlite3_column_text, but one thing you may want to do is when you call the std:string constructor, you'll want to cast to (const char*). I believe that it should have a constructor for that type.

However, it is odd that this sqlite function is return an unsigned char*, is it returning a Pascal string (first char is the length of the string)? If so, then you'll have to create the std::string with the bytes and the length.

1
  • No, they are null terminated.
    – Navin
    Mar 30, 2014 at 4:01
3

if temp_doc.uuid is a std::string try :

temp_doc.uuid = static_cast<const char*>(sqlite3_column_text(this->stmts.read_documents, 0));
1
  • It is a std::string, and trying out your method gives me: 1>.\storage_manager.cpp(109) : error C2440: 'static_cast' : cannot convert from 'const unsigned char *' to 'const char *' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    – LM.
    Apr 29, 2009 at 20:39
3

try:

temp_doc.uuid = std::string(reinterpret_cast<const char*>(sqlite3_column_text(this->stmts.read_documents, 0)));
4
  • I saw someone already posted the answer , I started to write this answer before the other was posted(was checking that the answer is correct in VS)
    – user88637
    Apr 29, 2009 at 20:41
  • I am only saying it so I won't think I copy answers
    – user88637
    Apr 29, 2009 at 20:42
  • I am only saying it so you won't think I copy answers
    – user88637
    Apr 29, 2009 at 20:42
  • const char* and const unsigned char* are not the same type. string constructor expect const char* and therefor a cast is needed
    – user88637
    Apr 29, 2009 at 20:43
2

You can't construct a std::string from const unsigned char* -- you have to cast it to const char* first:

temp_doc.uuid = std::string( reinterpret_cast< const char* >(
  sqlite3_column_text(this->stmts.read_documents, 0) ) );
1
  • sorry, have to use reinterpret_cast instead of static_cast.
    – Kasprzol
    Apr 29, 2009 at 20:44
2

I'm no expert but this example here seems much simpler:

string name = (const char*) (sqlite3_column_text(res, 0));
1
  • Don't use C cast in C++, it's a bad practice. A reinterpret_cast will show your intention more clearly.
    – Jean
    Jul 19, 2018 at 8:41
1

An old but important question, if you have to preserve the full information in the unsigned char sequence. In my opinion that is with reinterpret_cast not the case. I found an interesting solution under converting string to vector which I modified to

basic_string<unsigned char> temp = sqlite3_column_text(stmt, 0);
string firstItem( temp.begin(), temp.end() );

Since I am programming for gtkmm, you can realize the conversion into a Glib::ustring with

basic_string<unsigned char> temp = sqlite3_column_text(stmt, 0);
Glib::ustring firstItem = string( temp.begin(), temp.end() );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.