• 积累:Qt 多种数据类型之间的转换方法



    前言


    开发时经常涉及到数据类型的转换,为方便温故知新、提升开发效率,现将 Qt 开发部分常用的数据类型转换方式形成工具文档供查询、参考。


    1. int 转 QString


    1)函数:QString::number

    2)函数原型

    	//将数字(整数、浮点数、有符号、无符号等)转换为QString类型
    	//根据指定的基数返回与数字n等价的字符串。基数默认为10,取值范围必须在2到36之间。对于非10的基数,n被视为无符号整数。
    	static QString number(int n, int base=10);
        static QString number(uint n, int base=10);
        static QString number(long n, int base=10);
        static QString number(ulong n, int base=10);
        static QString number(qlonglong n, int base=10);
        static QString number(qulonglong n, int base=10);
        
    	//返回数字n的等效字符串,根据指定的格式和精度进行格式化。有关详细信息,请参见参数格式。
    	//与QLocale::toString()不同,此函数不遵循用户的语言环境设置。
        static QString number(double n, char f='g', int prec=6);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12


    3)参数说明:
    参数1:待转换数字
    参数2:要转换的目标进制
    参数3:如果参数2为浮点数为前提,参数3表示要保留小数位数

    默认情况下是十进制显示方式转换,也可以使用八进制、十六进制显示方式调用。


    4)使用示例:

    //整型转换成字符串
    int iNum = 100;
    QString qstrNum = QString::number(iNum, 10);			// qstrNum == “100”
    
    //整型转换成16进制字符串类型
    long lNum = 63;
    QString sNum = QString::number(lNum , 16);             	// sNum == "3f"
    QString tNum = QString::number(lNum , 16).toUpper();   	// tNum== "3F"
    
    //浮点类型转换成字符串并保留2位小数
    float fNum=4.567;
    QString sfNum = QString::number(fNum,‘f’, 2);			//sfNum  == "4.56"
    
    //整数转换为字符串,并保留3位,不足在前面补0,比如9转换成字符串009:
    int iSrc = 9;
    QString str1 = QString::number(iSrc).sprintf("%03d",iSrc);	//str1 == "009"
    QString str2 = QString("%1").arg(iSrc, 3, 10, QChar('0'));	//str2 == "009"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17


    2. QString 转 int


    1)函数:QString::toInt

    2)函数原型

    //返回使用base基准转换为int型的字符串,默认值为10,必须介于2到36之间,或者0。如果转换失败,返回0。
    int QString::toInt(bool *ok = nullptr, int base = 10) const
    
    • 1
    • 2


    3)参数说明:
    参数1:返回的执行结果,如果ok不是nullptr,则将ok设置为false表示失败,将ok设置为true表示成功。
    参数2:要转换的目标进制。

    参数2默认情况下是十进制显示方式转换,也可以使用八进制、十六进制显示方式调用。如果基数为0,则使用C语言惯例:如果字符串以"0x"开头,则使用基数16;如果字符串以“0”开头,则使用进制8;否则,默认以10为基准。

    字符串转换将始终在’C’语言环境中进行。对于语言环境相关的转换,使用QLocale::toInt()。


    4)使用示例:

     	QString number = "123";
        int inum = number.toInt();			//inum  == 123
    
    	QString str = "FF";
    	bool ok;
    	int hex = str.toInt(&ok, 16);       // hex == 255, ok == true
    	int dec = str.toInt(&ok, 10);       // dec == 0, ok == false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7


    3. std::string 转 QString


    1)函数:QString::fromLocal8Bit

    2)函数原型

    //Returns a QString initialized with the first size characters of the 8-bit string str.
    //If size is -1 (default), it is taken to be strlen(str).
    QString QString::fromLocal8Bit(const char *str, int size = -1)
    
    • 1
    • 2
    • 3


    3)参数说明:
    参数1:要转换的源数据。
    参数2:要转换的长度,默认为-1表示全部转换。


    4)使用示例:

    	//全部转换
     	std::string xhjName = "bcd";
        QString xhj = QString::fromLocal8Bit(xhjName.c_str());		//xhj == "bcd"
    	
    	//指定长度转换
    	std::string xName = "123456";
        QString qsName = QString::fromLocal8Bit(xName.c_str(),3);	//qsName == "123"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7


    4. QString 转 std::string


    1)函数:QString::toStdString

    2)函数原型

    //Returns a std::string object with the data contained in this QString. 
    //The Unicode data is converted into 8-bit characters using the toUtf8() function.
    std::string QString::toStdString() const
    
    • 1
    • 2
    • 3


    3)参数说明:


    4)使用示例:

    	QString qsName = "123456";
        std::string str = qsName.toStdString();		//str == "123456"
    
    • 1
    • 2


    5. QString 转 const char*


    1)函数:QString::toStdString

    2)函数原型

    //Returns a std::string object with the data contained in this QString. 
    //The Unicode data is converted into 8-bit characters using the toUtf8() function.
    std::string QString::toStdString() const
    
    • 1
    • 2
    • 3


    3)参数说明:


    4)使用示例:

        QString qsStr = "abc";
        const char* dst = qsStr.toLocal8Bit().data();
    
        std::string strarr1 = qsStr.toStdString();
        const char *arr0 = strarr1.c_str();				//arr0 == "abc"
        const char *arr1 = strarr1.data();				//arr1 == "abc"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6


    6. QString 转 char*


    1)函数:QString::toLatin1

    2)函数原型

    //Returns a Latin-1 representation of the string as a QByteArray.
    //The returned byte array is undefined if the string contains non-Latin1 characters. Those characters may be suppressed or replaced with a question mark.
    QByteArray QString::toLatin1() const
    
    • 1
    • 2
    • 3


    3)参数说明:


    4)使用示例:

    	QString qsStr = "abc";
    
        QByteArray ba = qsStr.toLatin1();
        char *arr3 = ba.data();             //arr3 == "abc"
    
    • 1
    • 2
    • 3
    • 4


    7. QString 转 ASCII 码


    1)函数:QString::toLatin1

    2)函数原型

    //Returns a Latin-1 representation of the string as a QByteArray.
    //The returned byte array is undefined if the string contains non-Latin1 characters. Those characters may be suppressed or replaced with a question mark.
    QByteArray QString::toLatin1() const
    
    • 1
    • 2
    • 3


    3)参数说明:


    4)使用示例:

    	QString str = "abc";
        QByteArray array = str .toLatin1();
    
        for (int i = 0; i < array.size(); ++i)
        {
            int ich= (int)array.at(i);                   // ich == 97 98 99
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7


    8. ASCII 转 QString


    示例

    	//int转对应ASCII的字符串,如转65为A字符串
    	QString str = (QChar)(65);		//str == "A"
    
    	//ASCII 数值 97 98 99 转成 a b c
        QByteArray array;
    	array.append(97);
    	array.append(98);
    	array.append(99);
    	 
    	for (int i = 0; i < array.size(); ++i)
    	{
    	    QString dst = QChar(array.at(i));		// 'a' 'b' 'c'
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    MATLAB算法实战应用案例精讲-【人工智能】ROS机器人(最终篇)
    安装Scala
    Vue中使用VueAMap
    时间序列预测—双向LSTM(Bi-LSTM)
    go 通过hash,查币安链交易代码
    彻底搞懂dfs与回溯
    算法通关村第十一关青铜挑战——移位运算详解
    状态模式
    Mac -- zsh-最新全网超详细的个性化终端(Terminal)颜色及vim颜色配置(亲测可行)
    内网-2(代理)
  • 原文地址:https://blog.csdn.net/locahuang/article/details/136291889