在 CPP 中,Qt5 有一個用於處理字符串的 QString 類。它非常強大並且有很多方法,通過 QString 類可以使用 Unicode 字符串。字符串保存為 16 位 QChar。每個 Unicode 4.0 字符由一個 QChar 表示。與許多其他編程語言中的字符串不同,QString 是可以更改的。現在我們將 Qstring 轉換為十六進製值。
轉換概念
我們將簡單地使用一個 Q 字符串並使用 Qstring 類的 “toInt” 預構建方法將其轉換為十六進製,然後提供一個 OK 指針來檢查轉換是否已成功執行。 Ok指針僅用於檢查錯誤,不會影響QString int到HexaDecimal的轉換。
示例
C++14
// use the qtcore tool to run this code
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaring
// the string
QString s;
// taking input
// from user
cin >> s;
// ok: pointer
bool ok;
// 16 hexa base
int hexa_dec = s.toInt(&ok, 16);
if (!ok) {
// conversion not done
cout << "Conversion failed. Repeat conversion"
<< endl;
}
else {
// printing the hexadecimal value
cout << "your Hexadecimal conversion of given "
"QString "
<< hexa_dec << endl;
}
return 0;
}
輸出:
s="FF" Your Hexadecimal Conversion of given QString 255 //Pointer 'OK' is true
結論
在QString中,為了存儲未處理的字節和傳統的8位“0”終止的字符串,除了QString之外,它還提供了QByteArray類。 QString 是大多數應用程序都應該使用的類。 Qt API 廣泛使用它,並且對 Unicode 的支持可確保您的應用程序在您希望擴大產品市場時易於翻譯。當存儲原始二進製數據和內存保護至關重要時,QByteArray 是這兩種情況下最合適的選擇(例如用於嵌入式 Linux 的 Qt)。
相關用法
- C++ Queue back()用法及代碼示例
- C++ Queue emplace()用法及代碼示例
- C++ Queue empty()用法及代碼示例
- C++ Queue pop()用法及代碼示例
- C++ Queue push()用法及代碼示例
- C++ Queue size()用法及代碼示例
- C++ Queue queue()用法及代碼示例
- C++ Queue front()用法及代碼示例
- C++ Queue swap()用法及代碼示例
- C++ Queue priority_queue()用法及代碼示例
- C++ Queue top()用法及代碼示例
- C++ Queue和Deque的區別用法及代碼示例
- C++ cos()用法及代碼示例
- C++ sin()用法及代碼示例
- C++ asin()用法及代碼示例
- C++ atan()用法及代碼示例
- C++ atan2()用法及代碼示例
- C++ acos()用法及代碼示例
- C++ tan()用法及代碼示例
- C++ sinh()用法及代碼示例
- C++ ceil()用法及代碼示例
- C++ tanh()用法及代碼示例
- C++ fmod()用法及代碼示例
- C++ acosh()用法及代碼示例
- C++ asinh()用法及代碼示例
注:本文由純淨天空篩選整理自divyanshojha2003大神的英文原創作品 How To Convert a Qstring to Hexadecimal in C++?。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。