Qt String Conversion and processing-18 bend road

Source: Internet
Author: User

The company recently asked to use Qt to write programs. The most depressing thing is the conversion between strings. Let's do it yourself! Fortunately, I am able to handle the problem... I also tried several built-in Qt conversion methods.

#include <QtGui/QApplication>#include "testqstring.h"#include "boost/lexical_cast.hpp"#include <iostream>#include <string>#include <stdio.h>#include <QDebug>using namespace std;int main(int argc, char *argv[]){    QApplication a(argc, argv);    testQString w;    w.show();    QString qStr="testString";    qDebug()<<"wyz-------1:"<<qStr;    string str=qStr.toStdString();    cout<<"wyz-------2:"<<str<<endl;    char *cStr;    cStr=(char*)str.c_str();    cout<<"wyz-------3:"<<cStr<<endl;    string str_1="testString2";    QString qStr_1;    qStr_1.sprintf("%s",str_1.c_str());    qDebug()<<"wyz-------4:"<<qStr_1;    return a.exec();}

Running result:

Starting/root/testQString-build-desktop/testQString...
Wyz ------- 1: "testString"
Wyz ------- 2: testString
Wyz ------- 3: testString
Wyz ------- 4: "testString2"

 

Convert int to QString
QString & sprintf (const char * cformat ,...)

String strData = write1.write (smsJson );
StrData = boost: erase_all_copy (strData, "\ n ");

 

String
StrTemp =
M_qStrInfo.toStdString ();

vector<string > splitVec;
boost::split(splitVec, strTemp, boost::is_any_of("\r\n"));

 

 

 

========= Copy the documents from the online copy ====================

// Common parameter types: char * string, QByteArray character array, and QString string
// Need to be converted: char * --- convert --- QByteArray --- the constructor of the QByteArray class needs to be called
Char * -------------- QByteArray (const char *) -----------> QByteArray
Char * str;
QByteArray byte (str );

Char * -------------- QString (const char *) ------------> QString
Char * str;
QString string (str );

QByteArray ------------- data () --------------------> char *
QByteArray byte;
Char * str = byte. data ();

QByteArray ------------ QString () -------------------> QString
QByteArray byte;
QString string (byte );

QString -------------- toAscii () ------------------> QByteArray
QString string;
QByteArray byte = string. toAscii ();

QString ------------- qPrintable () ------------------> char *
QString string;
Char * str = qPrintable (string );

QString s2q (const string & s)
{
Return QString (QString: fromLocal8Bit (s. c_str ()));
}
String q2s (const QString & s)
{
Return string (const char *) s. toLocal8Bit ());
}

QByteArray qba = m_pTcpSocket-> readAll ();
QString strings (qba); // convert to a qt string

QByteArray qba = tcpSocket-> readAll ();
Char * readStr = qba. data (); // convert QByteArray to char

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.