ID=dateLine->text();//得到字符串
QString str = ID;
bool ok;
int hex = str.toInt(&ok, 16); // 將字符串變成16機制數
hex=hex+1;
QString t = QString::number(hex, 16).toUpper();將數字變成16機制數,並以大寫顯示
dateLine->setText(t);顯示在textveiw() 裏面
cout<< hex<<endl;
16進制轉換。
這是一段非常經典的代碼,主要用在字符串與 int 類型的轉換。
實現功能:
1、將字符串按照16進制數,自增,只用修改
int hex = str.toInt(&ok, 10);
hex=hex+1;
2、將字符串按照10進制數,自增
int hex = str.toInt(&ok, 16);
hex=hex+1;
3、將字符串轉成16進制數,然後以10進制數顯示。
int hex = str.toInt(&ok, 16);
QString t = QString::number(hex, 10).toUpper();
4、將字符串轉成10進制數,然後以16進制數顯示
int hex = str.toInt(&ok, 10);
QString t = QString::number(hex, 16).toUpper();
原理:
int QString::toInt(bool * ok = 0, int base = 10) const
Returns the string converted(轉換) to an int using base base, which is 10 by default and must be between 2 and 36(轉換進制數,只能是2到36,二進制,10進制 ,9進制等都能夠轉換了), or 0. Returns 0 if the conversion fails.
If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
轉換成功,返回值OK爲1,返回 錯誤,則值爲1.所以 還可以對OK值進行 利用判斷,轉換是否成功。
If base is 0, the C language convention is used: If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toInt()
yearLine->setText((QString::number(CMD.trimmed().toInt(), 10)