Open In App

How To Convert a Qstring to Hexadecimal in C++?

Last Updated : 01 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In CPP Qt5 has a QString class for working with strings. It is very powerful and has numerous methods, A Unicode character string is made available via the QString class. A string is kept as a 16-bit QChar. Unicode 4.0 characters are represented by one QChar each. A QString can be changed, unlike strings in many other programming languages. Now we will convert Qstring in Hexa Decimal values.

Conversion Concept

We will simply use a Q string and convert it in Hexadecimal using the “toInt” Pre-built Method of Qstring Class then provide an OK pointer to check whether the conversion has been successfully executed. Ok pointer is just used for checking errors it will not affect the conversion of QString int to HexaDecimal.

Example

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;
}


Output:  

s="FF"
Your Hexadecimal Conversion of given QString 255  //Pointer 'OK' is true 

Conclusion

In QString For storing unprocessed bytes and conventional 8-bit “0”-terminated Strings, It offers the QByteArray class in addition to QString. QString is the class that should be used for the majority of applications. The Qt API uses it extensively, and the support for Unicode assures that your apps will be simple to translate in the event that you ever wish to broaden the market for your product. When storing raw binary data and memory conservation is crucial, QByteArray is the most appropriate choice in these two situations (For Example with Qt for Embedded Linux).


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads