QT、MFC 格式转换QString、CString转char*

QT数据格式转换方式

  • int  转 QString

int m=1;

QString b;

b=QString::number(m)

  • QString 转int

QString a="1111"

int b;

b=a.toInt()

  • char 转换为 QString

char a='b';

QString(QLatin1String(a));

QString 转换为 char

QString str="qq";

char *ch;

ch = str.toLatin1.data();

char * 与 const char *的转换

char *ch1="tt";

const char *ch2="qq";

ch2 = ch1;//不报错,但有警告

ch1 = (char *)ch2;

QByteArray 转换为 char *

char *ch;//不要定义成ch[n];

QByteArray byte;

ch = byte.data();

char * 转换为  QByteArray

char *ch;

QByteArray byte;

byte = QByteArray(ch);

QString 转换为  QByteArray

QByteArray byte;

QString string;

byte = string.toAscii();

QByteArray 转换为  QString

QByteArray byte;

QString string;

string = QString(byte);

MFC CString 转为char*

CString strSource;//声明CString

char* charSource; //声明char*

法1:

charSource = (char*)strSource.GetBuffer(0);

法2:

charSource = (char*)strSource.GetBuffer(strSource.GetLength());

法3:

charSource = (char*)(LPCTSTR)strSource

(0)

相关推荐