C++ const unsigned char * 到 std::string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/804123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
const unsigned char * to std::string
提问by LM.
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.
sqlite3_column_text 返回一个 const unsigned char*,如何将其转换为 std::string?我试过 std::string(),但出现错误。
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
回答by Reunanen
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.
虽然std::string
可以有一个带 的构造函数const unsigned char*
,但显然它没有。
Why not, then? You could have a look at this somewhat related question: Why do C++ streams use char instead of unsigned char?
那为什么不呢?你可以看看这个有点相关的问题:为什么 C++ 流使用 char 而不是 unsigned char?
回答by LM.
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);
回答by Mike Mueller
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.
人们通常使用 (unsigned char *) 类型的原因是为了表明数据是二进制而不是纯 ASCII 文本。我知道 libxml 会这样做,从它的外观来看,sqlite 也在做同样的事情。
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.
您从 sqlite 调用返回的数据可能是 UTF-8 编码的 Unicode 文本。虽然 reinterpret_cast 可能看起来有效,但如果有人在非纯 ASCII 字段中存储文本,您的程序可能不会表现良好。
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.
std::string 类的设计并没有考虑到 Unicode,所以如果你要求一个字符串的 length(),你会得到字节数,在 UTF-8 中,这不一定是一回事作为字符数。
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.
简短回答:如果您确定数据只是 ASCII,简单的转换可能会起作用。如果它可以是任何 UTF-8 数据,那么您需要以更智能的方式处理编码/解码。
回答by Lyndsey Ferguson
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.
我不熟悉 sqlite3_column_text,但您可能想要做的一件事是,当您调用 std:string 构造函数时,您需要强制转换为 (const char*)。我相信它应该有一个该类型的构造函数。
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.
然而,奇怪的是这个 sqlite 函数返回一个无符号字符*,它返回一个 Pascal 字符串(第一个字符是字符串的长度)?如果是这样,那么您必须使用字节和长度创建 std::string。
回答by user88637
try:
尝试:
temp_doc.uuid = std::string(reinterpret_cast<const char*>(sqlite3_column_text(this->stmts.read_documents, 0)));
回答by Kasprzol
You can't construct a std::string
from const unsigned char*
-- you have to cast it to const char*
first:
您不能std::string
从 c构造 a onst unsigned char*
- 您必须const char*
先将其转换为:
temp_doc.uuid = std::string( reinterpret_cast< const char* >(
sqlite3_column_text(this->stmts.read_documents, 0) ) );
回答by Raoul Supercopter
if temp_doc.uuid is a std::string try :
如果 temp_doc.uuid 是 std::string 尝试:
temp_doc.uuid = static_cast<const char*>(sqlite3_column_text(this->stmts.read_documents, 0));
回答by still_learning
I'm no expert but this example here seems much simpler:
我不是专家,但这里的这个例子似乎更简单:
string name = (const char*) (sqlite3_column_text(res, 0));
回答by Erich Kuester
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 vectorwhich I modified to
一个古老但重要的问题,如果您必须在 unsigned char 序列中保留完整信息。在我看来,reinterpret_cast 并非如此。我在将字符串转换为向量下找到了一个有趣的解决方案 ,我将其修改为
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
由于我正在为 gtkmm 编程,因此您可以使用以下方法实现转换为 Glib::ustring
basic_string<unsigned char> temp = sqlite3_column_text(stmt, 0);
Glib::ustring firstItem = string( temp.begin(), temp.end() );