Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the conversion between binary, Octal and hexadecimal by QT

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to achieve the conversion between binary, octal and hexadecimal in QT". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "how to achieve the conversion between binary, octal and hexadecimal in QT".

There are three main methods used in QT.

The first is QString::number (int n, int base = 10)

The second is QString::setNum (short n, int base = 10)

The third is int QString::toInt (bool * ok = nullptr, int base = 10) const

The default values for all three methods are decimal.

The effect picture will be added first, and finally the source code will be attached:

Next, start the code implementation:

First open QT- > create a new file or project, and then follow the annotations in the figure to proceed to the next step

The file name and path can be set by yourself.

Keep clicking on the next step

Keep clicking on the next step. Click the green arrow to run it first.

And here comes the big show!

As shown in the figure, the function definition is also added to the .cpp file:

The function to be realized is that when you click the corresponding "convert to other base" button, get the contents of the corresponding input box, and then convert the contents to the corresponding base.

Main hao

/ / both QString::number () and setNum () can convert void MainWindow::on_btn1_clicked () {/ / decimal to other decimal QString str = ui- > shi- > text (); int value = str.toInt (); / / decimal, toInt () defaults to decimal number str = str.setNum (value,2); / / to binary ui- > er- > setText (str); str = str.setNum (value,16). ToUpper () / / convert to hexadecimal ui- > shiliu- > setText (QString ("0x%1") .arg (str)); str = str.setNum (value,8); / / convert octal ui- > ba- > setText (str);} void MainWindow::on_btn2_clicked () {/ / binary to other binary QString str = ui- > er- > text (); / / binary bool ok; int value = str.toInt (& ok, 2) / / read successfully ok=true; qDebug () setText (str); str = QString::number (value,16). ToUpper (); / / converted to hexadecimal ui- > shiliu- > setText (QString ("0x%1") .arg (str)); str = QString::number (value,8); / / converted to octal ui- > ba- > setText (str) } void MainWindow::on_btn3_clicked () {/ / hexadecimal to other binary QString str = ui- > shiliu- > text (); / hexadecimal bool ok; int value = str.toInt (& ok, 16); / / read str = QString::number (value,10) in hexadecimal number; / / convert to decimal ui- > shi- > setText (str); str = str.setNum (value,2) / / convert to binary ui- > er- > setText (str); str = QString::number (value,8); / / convert octal ui- > ba- > setText (str);} void MainWindow::on_btn4_clicked () {/ / Octal to other binary QString str = ui- > ba- > text (); / / Octal bool ok; int value = str.toInt (& ok, 8) / / read str = QString::number (value,10) in octal; / / convert to decimal ui- > shi- > setText (str); str = str.setNum (value,2); / / convert to binary ui- > er- > setText (str); str = QString::number (value,16). ToUpper (); / / convert to hexadecimal ui- > shiliu- > setText (QString ("0x%1") .arg (str));}

Well, at this point, the code is over, doesn't it feel very simple?

Finally, attach the source code, personal test can be run, if you are running, there is a problem, you can leave a message.

.pro file source code

QT + = core gui greaterThan (QT_MAJOR_VERSION, 4): QT + = widgets CONFIG + = 11 # The following define makes your compiler emit warnings if you use# any Qt feature that has been marked deprecated (the exact warnings# depend on your compiler). Please consult the documentation of the# deprecated API in order to know how to port your code away from it.DEFINES + = QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs.# In order to do so Uncomment the following line.# You can also select to disable deprecated APIs only up to a certain version of Qt.#DEFINES + = QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES + =\ main.cpp\ mainwindow.cpp HEADERS + =\ mainwindow.h FORMS + =\ mainwindow.ui # Default rules for deployment.qnx: target.path = / tmp/$$ {TARGET} / binelse: UnixPart Android: target.path = / opt/$$ {TARGET} / binbinisEmpty (target.path): INSTALLS + = target

Header file .h source code

# ifndef MAINWINDOW_H#define MAINWINDOW_H# include QT_BEGIN_NAMESPACEnamespace Ui {class MainWindow;} QT_END_NAMESPACE class MainWindow: public QMainWindow {Q_OBJECT public: MainWindow (QWidget * parent = nullptr); ~ MainWindow (); private slots: void on_btn1_clicked (); void on_btn2_clicked (); void on_btn3_clicked (); void on_btn4_clicked (); private: Ui::MainWindow * ui;}; # endif / / MAINWINDOW_H

Main.cpp source code

# include "mainwindow.h" # include int main (int argc, char * argv []) {QApplication a (argc, argv); MainWindow w; w.show (); return a.exec ();}

.CPP source code

# include "mainwindow.h" # include "ui_mainwindow.h" # include MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); this- > setWindowTitle ("conversion between various binary systems");} MainWindow::~MainWindow () {delete ui } / / QString::number () and setNum () can both convert void MainWindow::on_btn1_clicked () {/ / decimal to other decimal QString str = ui- > shi- > text (); int value = str.toInt (); / / decimal, toInt () defaults to decimal number str = str.setNum (value,2); / / to binary ui- > er- > setText (str); str = str.setNum (value,16). ToUpper () / / convert to hexadecimal ui- > shiliu- > setText (QString ("0x%1") .arg (str)); str = str.setNum (value,8); / / convert octal ui- > ba- > setText (str);} void MainWindow::on_btn2_clicked () {/ / binary to other binary QString str = ui- > er- > text (); / / binary bool ok; int value = str.toInt (& ok, 2) / / read successfully ok=true; qDebug () setText (str); str = QString::number (value,16). ToUpper (); / / converted to hexadecimal ui- > shiliu- > setText (QString ("0x%1") .arg (str)); str = QString::number (value,8); / / converted to octal ui- > ba- > setText (str) } void MainWindow::on_btn3_clicked () {/ / hexadecimal to other binary QString str = ui- > shiliu- > text (); / hexadecimal bool ok; int value = str.toInt (& ok, 16); / / read str = QString::number (value,10) in hexadecimal number; / / convert to decimal ui- > shi- > setText (str); str = str.setNum (value,2) / / convert to binary ui- > er- > setText (str); str = QString::number (value,8); / / convert octal ui- > ba- > setText (str);} void MainWindow::on_btn4_clicked () {/ / Octal to other binary QString str = ui- > ba- > text (); / / Octal bool ok; int value = str.toInt (& ok, 8) / / read str = QString::number (value,10) in octal; / / convert to decimal ui- > shi- > setText (str); str = str.setNum (value,2); / / convert to binary ui- > er- > setText (str); str = QString::number (value,16). ToUpper (); / / convert to hexadecimal ui- > shiliu- > setText (QString ("0x%1") .arg (str));}

The running interface is as follows:

Thank you for your reading, the above is the content of "how to achieve the conversion between binary, octal and hexadecimal in QT". After the study of this article, I believe you have a deeper understanding of how to achieve the conversion between binary, octal and hexadecimal in QT, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report