C++ 可转换为‘;QStringList’;至‘;无符号字符’;?
我有一个QStringList,我想把它转换成无符号字符[32] 有可能吗?我该怎么做 例如:C++ 可转换为‘;QStringList’;至‘;无符号字符’;?,c++,qt,C++,Qt,我有一个QStringList,我想把它转换成无符号字符[32] 有可能吗?我该怎么做 例如: publicKey = "0x46,0x9e,..." auto queryPK = publicKey.split(','); 这就是我的qDebug() 这就是我转换后想要的: unsigned char pk = {0x46,0x9e,...} 假设您的QStringList包含以下内容: QStringList sl = QStringList() << "0x46" <
publicKey = "0x46,0x9e,..."
auto queryPK = publicKey.split(',');
这就是我的qDebug()
这就是我转换后想要的:
unsigned char pk = {0x46,0x9e,...}
假设您的QStringList包含以下内容:
QStringList sl = QStringList() << "0x46" << "0x9e";
QStringList sl=QStringList()尝试使用toAscii().constData()
请注意,您必须使用以下代码删除序列:
delete [] sequence
您必须添加拆分逻辑和循环才能获得完整的解决方案。您可以按中所述迭代列表
因此,基本上您需要这样的东西(假设queryPK
是您的QStringList
对象):
std::vector v;
常量迭代器;
for(constitterator=queryPK.constBegin();constitterator!=queryPK.constEnd();
++便秘器)
v、 后侵位(性病:斯托尔(便秘者));
但是您必须确保constriterator
的长度为1字节,否则您将得到窄化转换什么意思,32个位置的QStringList?不,QStringList是一个对象,无符号字符[32]是一个32个字符的数组。我想你真正的意思是如何从字符串列表中获取数组?我想你可以从这里开始看:这段代码生成nosense。(const auto&s,stringList)int i=s.toInt()的无效
,假设字符串列表中的每个元素都可以转换为整数值。sizeof错误,通常不是OP试图实现的。更好,但是toUInt
的第二个参数应该是0(因为您希望它解析0x
前缀);此外,您还应该通过第一个参数检查转换是否成功…@MatteoItalia Fixed;)@ThibautB。先生。。。这解决了我的问题!!这不是他想要实现的,但这是他想要实现的一部分,我在回答中也提到了这一点。
unsigned char pk[32];
int idx = 0;
for (const QString& s : sl){
// transform to uint
bool converted = false;
unsigned char uc = static_cast<unsigned char>(s.toUInt(&converted, 0));
if (converted){
pk[idx] = uc;
}
idx++;
}
QString str = "ABCD";
int length = str.length();
unsigned char *sequence = NULL;
sequence = (unsigned char*)qstrdup(str.toAscii().constData());
delete [] sequence
std::vector<unsigned char> v;
QStringList::const_iterator constIterator;
for (constIterator = queryPK.constBegin(); constIterator != querypk.constEnd();
++constIterator)
v.emplace_back(std::stoul(constIterator));