Use QT and C language or C++ language to realize the conversion between decimal, binary, octal, and hexadecimal (two methods)

method one:

Mainly use three methods in QT.

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

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

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

The default values ​​of these three methods are decimal.

First upload the renderings, and finally attach the source code:

Next, start the code implementation:

First open QT->New file or project, and then proceed to the next step as indicated in the figure

The file name and path can be set by yourself.

Keep clicking Next;

Keep clicking Next. If the creation is successful, click the green arrow to run it.

Then comes the main event! ! ! !

 

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

 The function to be realized is to obtain the content of the corresponding input box when the button corresponding to "convert to other bases" is clicked, and then convert the content to the corresponding base.

main hao

//QString::number()和setNum()都可以转换
void MainWindow::on_btn1_clicked()
{//十进制转为其他进制
    QString str = ui->shi->text();
    int value = str.toInt();//十进制,toInt()默认是10进制数

    str = str.setNum(value,2);//转为二进制
    ui->er->setText(str);

    str = str.setNum(value,16).toUpper();//转为十六进制
    ui->shiliu->setText(QString("0x%1").arg(str));

    str = str.setNum(value,8);//转为八进制
    ui->ba->setText(str);
}

void MainWindow::on_btn2_clicked()
{//二进制转为其他进制
    QString str = ui->er->text();//二进制
    bool ok;
    int value = str.toInt(&ok, 2);//以二进制数读入,读取成功ok=true;
    qDebug() << "ok=" << ok;

    str = QString::number(value,10);//转为十进制
    ui->shi->setText(str);

    str = QString::number(value,16).toUpper();//转为十六进制
    ui->shiliu->setText(QString("0x%1").arg(str));

    str = QString::number(value,8);//转为八进制
    ui->ba->setText(str);
}

void MainWindow::on_btn3_clicked()
{//十六进制转为其他进制
    QString str = ui->shiliu->text();//十六进制
    bool ok;
    int value = str.toInt(&ok, 16);//以十六进制数读入

    str = QString::number(value,10);//转为十进制
    ui->shi->setText(str);

    str = str.setNum(value,2);//转为二进制
    ui->er->setText(str);

    str = QString::number(value,8);//转为八进制
    ui->ba->setText(str);
}

void MainWindow::on_btn4_clicked()
{//八进制转为其他进制
    QString str = ui->ba->text();//八进制
    bool ok;
    int value = str.toInt(&ok, 8);//以八进制数读入

    str = QString::number(value,10);//转为十进制
    ui->shi->setText(str);

    str = str.setNum(value,2);//转为二进制
    ui->er->setText(str);

    str = QString::number(value,16).toUpper();//转为十六进制
    ui->shiliu->setText(QString("0x%1").arg(str));
}

 Ok, here is the end of the code, doesn't it feel very simple? !

Finally, the source code is attached, and the personal test can run. If you have any problems while running, you can leave a message.

.pro file source code

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++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}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

Header file.h source code

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace 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 <QApplication>

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 <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("各种进制之间相互转换");
}

MainWindow::~MainWindow()
{
    delete ui;
}

//QString::number()和setNum()都可以转换
void MainWindow::on_btn1_clicked()
{//十进制转为其他进制
    QString str = ui->shi->text();
    int value = str.toInt();//十进制,toInt()默认是10进制数

    str = str.setNum(value,2);//转为二进制
    ui->er->setText(str);

    str = str.setNum(value,16).toUpper();//转为十六进制
    ui->shiliu->setText(QString("0x%1").arg(str));

    str = str.setNum(value,8);//转为八进制
    ui->ba->setText(str);
}

void MainWindow::on_btn2_clicked()
{//二进制转为其他进制
    QString str = ui->er->text();//二进制
    bool ok;
    int value = str.toInt(&ok, 2);//以二进制数读入,读取成功ok=true;
    qDebug() << "ok=" << ok;

    str = QString::number(value,10);//转为十进制
    ui->shi->setText(str);

    str = QString::number(value,16).toUpper();//转为十六进制
    ui->shiliu->setText(QString("0x%1").arg(str));

    str = QString::number(value,8);//转为八进制
    ui->ba->setText(str);
}

void MainWindow::on_btn3_clicked()
{//十六进制转为其他进制
    QString str = ui->shiliu->text();//十六进制
    bool ok;
    int value = str.toInt(&ok, 16);//以十六进制数读入

    str = QString::number(value,10);//转为十进制
    ui->shi->setText(str);

    str = str.setNum(value,2);//转为二进制
    ui->er->setText(str);

    str = QString::number(value,8);//转为八进制
    ui->ba->setText(str);
}

void MainWindow::on_btn4_clicked()
{//八进制转为其他进制
    QString str = ui->ba->text();//八进制
    bool ok;
    int value = str.toInt(&ok, 8);//以八进制数读入

    str = QString::number(value,10);//转为十进制
    ui->shi->setText(str);

    str = str.setNum(value,2);//转为二进制
    ui->er->setText(str);

    str = QString::number(value,16).toUpper();//转为十六进制
    ui->shiliu->setText(QString("0x%1").arg(str));
}

The interface after running is as follows:

Method Two:

This method mainly uses QSpinBox and QDoubleSpinBox controls.

Both controls have the following properties:

prefix: the prefix of the number display;

suffix: the suffix of the digital display;

displayIntegerBase: Displays the base used by integers.

By setting the above three attributes, the conversion between bases can be realized. The first two can not be set, but the last one must be set to achieve the desired effect.

The most important thing is that after setting, QT will automatically display according to the effect, which is super convenient! ! !

Effect picture after running.

 The new project is the same as the above, or you can choose not to create a new project and use the above project directly.

First design the interface, as follows:

 

 

There is one octal left, I believe everyone will definitely set its three attributes.

After the properties are set, right-click the button and select Go to Slot, and the slot function definition and implementation will be automatically added to the header file and .cpp file.

Then there is the code to implement this function.

void MainWindow::on_pushButton_clicked()
{
    int value = ui->shi->value();
    ui->er->setValue(value);
    ui->shiliu->setValue(value);
    ui->ba->setValue(value);
}

void MainWindow::on_pushButton_2_clicked()
{
    int value = ui->er->value();
    ui->shi->setValue(value);
    ui->shiliu->setValue(value);
    ui->ba->setValue(value);
}

void MainWindow::on_pushButton_3_clicked()
{
    int value = ui->shiliu->value();
    ui->er->setValue(value);
    ui->shi->setValue(value);
    ui->ba->setValue(value);
}

void MainWindow::on_pushButton_4_clicked()
{
    int value = ui->ba->value();
    ui->er->setValue(value);
    ui->shi->setValue(value);
    ui->shiliu->setValue(value);
}

 After running, the effect will come out. At this point, the two methods are over. Use whichever you like. hey-hey.

Guess you like

Origin blog.csdn.net/m0_49456900/article/details/124760164