Qt int, float, double to qstring

Source: Internet
Author: User
Qt int, float, double to qstring

There are two ways of

1. Use

Qstring::number ();

Such as:

[CPP] View plaincopyprint?

1. Long A = 63;
2. QString s = Qstring::number (A, 10); s = = "63"
3. QString T = Qstring::number (A,). ToUpper (); t = = "3F"

Long A = 63;
QString s = Qstring::number (A, 10); s = = "63"
QString t = Qstring::number (A,). ToUpper (); t = = "3F"

(Explanation, variable A is int type or float,double.) 10 and 16 are in-system) ToUpper is uppercase

2. Use

[CPP] View plaincopyprint?

1. Long A = 63;
2. QString s = QString ("%1"). Arg (a);

Long A = 63;
QString s = QString ("%1"). Arg (a);

Well, I'm not used to it.


Convert qstring to double type

Method 1.

[CPP] View plaincopyprint?

1. QString str= "123.45";
2. Double val=str.todouble (); val=123.45

QString str= "123.45";
Double val=str.todouble (); val=123.45



Method 2. Very suitable for scientific notation form conversion

[CPP] View plaincopyprint?

1. bool OK;
2. Double D;
3. D=qstring ("1234.56e-02"). ToDouble (&ok); ok=true;d;12.3456.

BOOL OK;
Double D;
D=qstring ("1234.56e-02"). ToDouble (&ok); ok=true;d;12.3456.





Convert qstring to float

1.

[CPP] View plaincopyprint?

1. QString str= "123.45";
2. Float d=str.tofloat (); d=123.45

QString str= "123.45";
float d=str.tofloat (); d=123.45

2.

[HTML] View Plaincopyprint?

1. QString str= "R2D2";
2. bool OK;
3. Float d=str.tofloat (&ok); Conversion is when returned 0.0,ok=false;

QString str= "R2D2";
BOOL OK;
Float d=str.tofloat (&ok); Conversion is when returned 0.0,ok=false;

Convert the qstring shape into plastic

1. Convert to decimal shaping

Note: The cardinality defaults to 10. When the base is 10 o'clock, and the cardinality must be 2 to 36

Between If the cardinality is 0, if the string is beginning with the 0x will be converted to 16, if the beginning of 0 converted to octal, otherwise converted to decimal.

[CPP] View plaincopyprint?

1. Qstring str= "FF";
2. bool OK;
3. int Dec=str.toint (&ok,10); dec=255; Ok=rue
4. int hex =str.toint (&ok,16); Hex=255;ok=true;

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. The conversion of the regular plastic to qstring shape

[CPP] View plaincopyprint?

1. Long A = 63;
2. QString Str=qstring::number (a,16); Str= "3f";
3. QString Str=qstring::number (a,16). ToUpper (); Str= "3F";

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 One:
[CPP] View plaincopyprint?

1. QString qstr ("Hello,word");
2. const char * p = qstr.tolocal8bit (). data ();

QString qstr ("Hello,word");
const char * p = qstr.tolocal8bit (). data ();



Method Two:
[CPP] View plaincopyprint?

1. const char *p = qstr.tostdstring (). data ();

const char *p = qstr.tostdstring (). data ();



The conversion is constant.
Translate the current time into qstring ...

Public Qdatetime qdate = Qdatetime.currentdatetime ();
datetime = qdate.tostring ("YYYY year mm month DD Day Ddddhh:mm:ss");
If you are not qtime and qdate classes such as: Char unsigned char classes received via TCP/IP are converted to 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);

}




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.