Conversion between QString and numbers in Qt

Source: Internet
Author: User

Convert QString to double type

Method 1: QString str = "123.45 ";

Double val = str. toDouble (); /// val = 123.45

Method 2. Suitable for scientific notation form conversion

Bool OK;

Double d;

D = QString ("1234.56e-02"). toDouble (& OK); // OK = true; d; 12.3456.

Convert QString to float

1. QString string str = "123.45 ";

Float d = str. toFloat (); // d = 123.45

2. QString str = "R2D2 ";

Bool OK;

Float d = str. toFloat (& OK); // If the conversion is successful, 0.0 is returned. OK = false;

Convert QString to integer

1. Convert to decimal integer

Note: The default base is 10. When the base number is 10, and the base number must be between 2 and 36

. If the base number is 0, if the string starts with 0x, it is converted to hexadecimal notation. If it starts with 0, it is converted to octal notation. Otherwise, it is converted to decimal notation.

Qstring str = "FF ";

Bool OK;

Int dec = str. toInt (& OK, 10); // dec = 255; OK = rue

Int hex = str. toInt (& OK, 16); // hex = 255; OK = true;

3. Convert regular integer to Qstring

Long a = 63;

QString str = QString: number (a, 16); // str = "3f ";

QString str = QString: number (a, 16). toUpper (); // str = "3F ";

Qstring conversion char * problem!
Method 1:
QString qstr ("hello, word ");
Const char * p = qstr. toLocal8Bit (). data ();
Method 2:
Const char * p = qstr. toStdString (). data ();
The converted constant


Convert the current time to QString...
Public QDateTime qdate = QDateTime. currentDateTime ();
Datetime = qdate. toString ("yyyy-MM-dd ddddhh: mm: ss ");
If it is not a QTime or QDate class, for example, how to convert the char unsigned char class received through TCP/IP to a QString class?
QString Time2String (DWORD dwTime)
{
Char ctime [50] = {0 };
Memset (ctime, 0, 50 );
Strftime (ctime, 32, "% Y-% m-% d % H: % m: % s", localtime (& time_t (dwtime )));
Return qstring (ctime );

}

From: http://blog.163.com/zjf_to/blog/static/2014290612011112883629518/

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.