【问题标题】:Casting Qvector<uint8_t> to QString将 Qvector<uint8_t> 转换为 QString
【发布时间】:2020-06-10 01:21:07
【问题描述】:

我想将对应于字符的十六进制值的 uint8_t 向量转换为字符串。但我无法将我的向量转换为字符串。请帮忙 我的代码:

#include <QVector>
#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    QVector<uint8_t> vec;
    vec.push_back(51);
    vec.push_back(32);
    vec.push_back(123);
    QString message = "";
    qDebug()<<vec.at(0);

    for(int counter=0;counter<vec.length();counter++){
        message=vec.at(counter)+ message;
    }

    qDebug()<<message;
    //w.show();
    return a.exec();
}

【问题讨论】:

  • 你显示的代码有什么问题?你有构建错误吗?那么有哪些错误呢?它会给出错误的结果吗?那么你得到了什么结果,你期望什么?程序会崩溃吗?它在哪里崩溃?请花一些时间阅读How to Askthis question checklist,然后阅读edit 你的问题以改进它。
  • 至少你忘了将counter初始化为0。
  • Thx @Botje 我忘记了,但不幸的是结果是一样的

标签: c++ qt qstring qvector


【解决方案1】:

这样的事情可能会有所帮助(只需要将 std::vector 替换为 qvector):

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <stdint.h>
int main()
{
    std::vector<uint8_t> vec;
    vec.push_back(51);
    vec.push_back(32);
    vec.push_back(123);

    std::vector<char> result;

    for (auto v: vec)
    {
        char c = v;
        result.push_back(c);    
    }

    std::string s(result.begin(), result.end());
    std::cout << s;
}

【讨论】:

    【解决方案2】:
    #include <QCoreApplication>
    #include <QDebug>
    #include <algorithm>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QVector<uint8_t> vec;
        QString message;
        QByteArray hex;
        vec.push_back(65);
        vec.push_back(72);
        vec.push_back(73);
        vec.push_back(84);
    
        QByteArray data;
        std::copy( vec.begin() , vec.end() , std::back_inserter( data ) );
        hex=data.toHex();
        qDebug() << hex+ " ";
        qDebug()<<message.fromUtf8(data);
       return a.exec();
    }
    

    【讨论】:

      【解决方案3】:

      您的问题对我来说仍然很模糊,但据我了解,您希望 qDebug() 以十六进制输出数字。

      所以可以通过QByteAray 完成。可能的实现:

      #include <QVector>
      #include <QCoreApplication>
      #include <QDebug>
      #include <algorithm>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          QVector<uint8_t> vec;
          vec.push_back(51);
          vec.push_back(32);
          vec.push_back(123);
      
          QByteArray data;
          std::copy( vec.begin() , vec.end() , std::back_inserter( data ) );
      
          qDebug() << data.toHex( ' ' );
      
          return a.exec();
      }
      

      输出是:

      33 20 7b

      还可以使用QString::number( your_value , 16 ) 将单个数字转换为十六进制表示的字符串。这里的 16 代表base

      【讨论】:

      • 我的问题是将矢量变量转换为 ASCII 字符。这段代码至少给了我一个线索,非常感谢。
      • @BerkantAy qDebug() &lt;&lt; data; 将直接打印数字的 ascii 等价物。
      【解决方案4】:

      使用QString::fromUtf8()QString::fromLatin1() 立即进行转换。

      【讨论】:

      • 能否将其实现到示例中
      猜你喜欢
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 2019-12-23
      相关资源
      最近更新 更多