qt-qstring

string to qstring

qs = QString::fromStdString(s);

数字转字符串

int i = 42;
QString s = QString::number(i);

字符串转数字

QString Abcd = "123.5 Kb";
Abcd.split(" ")[0].toInt(); //convert the first part to Int
Abcd.split(" ")[0].toDouble(); //convert the first part to double
Abcd.split(" ")[0].toFloat(); //convert the first part to float

bool flag;
double v = Abcd.split(" ")[0].toDouble(&flag);
if(flag){
// use v
}

To char*

QString str1 = "Test";
QByteArray ba = str1.toLocal8Bit();
const char *c_str2 = ba.data();

QString转QByteArray


char convertCharToHex(char ch)
{
if((ch >= '0') && (ch <= '9'))
return ch-0x30;
else if((ch >= 'A') && (ch <= 'F'))
return ch-'A'+10;
else if((ch >= 'a') && (ch <= 'f'))
return ch-'a'+10;
else return (-1);
}

void convertStringToHex(const QString &str, QByteArray &byteData)
{
int hexdata,lowhexdata;
int hexdatalen = 0;
int len = str.length();
byteData.resize(len/2);
char lstr,hstr;
for(int i=0; i<len; )
{
//char lstr,
hstr=str[i].toLatin1();
if(hstr == ' ')
{
i++;
continue;
}
i++;
if(i >= len)
break;
lstr = str[i].toLatin1();
hexdata = convertCharToHex(hstr);
lowhexdata = convertCharToHex(lstr);
if((hexdata == 16) || (lowhexdata == 16))
break;
else
hexdata = hexdata*16+lowhexdata;
i++;
byteData[hexdatalen] = (char)hexdata;
hexdatalen++;
}
byteData.resize(hexdatalen);
}

QString testData="a503930000000200000008746ed16a156655a6";
QByteArray arr = m_socket->readAll();
arr = testData.toUtf8().toHex();
convertStringToHex(testData,arr);
char* p = (char*)arr.data();

char* to QByteArray

databuf = QByteArray((char*)buf, 10);