QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換
主要使用QT中的三個(gè)方法。
- 第一個(gè)是QString::number(int n, int base = 10);
- 第二個(gè)是QString::setNum(short n, int base = 10);
- 第三個(gè)是int QString::toInt(bool *ok = nullptr, int base = 10) const
這三個(gè)方法默認(rèn)值都是十進(jìn)制。
先上效果圖,最后會(huì)附上源碼:

接下來開始代碼實(shí)現(xiàn):
首先打開QT->新建文件或項(xiàng)目,然后跟著圖中標(biāo)注進(jìn)行下一步

文件名和路徑自己設(shè)置就可。

一直點(diǎn)下一步;

一直點(diǎn)下一步。創(chuàng)建成功先點(diǎn)綠色箭頭運(yùn)行一下。
接著重頭戲來了?。。?!


如圖所示,同時(shí)還會(huì)在.cpp文件中添加函數(shù)定義:

所要實(shí)現(xiàn)的功能是,當(dāng)點(diǎn)擊對(duì)應(yīng)“轉(zhuǎn)換為其他進(jìn)制”的按鈕時(shí),獲取對(duì)應(yīng)輸入框的內(nèi)容,然后把內(nèi)容轉(zhuǎn)換為對(duì)應(yīng)進(jìn)制。
主要hao
//QString::number()和setNum()都可以轉(zhuǎn)換
void MainWindow::on_btn1_clicked()
{//十進(jìn)制轉(zhuǎn)為其他進(jìn)制
QString str = ui->shi->text();
int value = str.toInt();//十進(jìn)制,toInt()默認(rèn)是10進(jìn)制數(shù)
str = str.setNum(value,2);//轉(zhuǎn)為二進(jìn)制
ui->er->setText(str);
str = str.setNum(value,16).toUpper();//轉(zhuǎn)為十六進(jìn)制
ui->shiliu->setText(QString("0x%1").arg(str));
str = str.setNum(value,8);//轉(zhuǎn)為八進(jìn)制
ui->ba->setText(str);
}
void MainWindow::on_btn2_clicked()
{//二進(jìn)制轉(zhuǎn)為其他進(jìn)制
QString str = ui->er->text();//二進(jìn)制
bool ok;
int value = str.toInt(&ok, 2);//以二進(jìn)制數(shù)讀入,讀取成功ok=true;
qDebug() << "ok=" << ok;
str = QString::number(value,10);//轉(zhuǎn)為十進(jìn)制
ui->shi->setText(str);
str = QString::number(value,16).toUpper();//轉(zhuǎn)為十六進(jìn)制
ui->shiliu->setText(QString("0x%1").arg(str));
str = QString::number(value,8);//轉(zhuǎn)為八進(jìn)制
ui->ba->setText(str);
}
void MainWindow::on_btn3_clicked()
{//十六進(jìn)制轉(zhuǎn)為其他進(jìn)制
QString str = ui->shiliu->text();//十六進(jìn)制
bool ok;
int value = str.toInt(&ok, 16);//以十六進(jìn)制數(shù)讀入
str = QString::number(value,10);//轉(zhuǎn)為十進(jìn)制
ui->shi->setText(str);
str = str.setNum(value,2);//轉(zhuǎn)為二進(jìn)制
ui->er->setText(str);
str = QString::number(value,8);//轉(zhuǎn)為八進(jìn)制
ui->ba->setText(str);
}
void MainWindow::on_btn4_clicked()
{//八進(jìn)制轉(zhuǎn)為其他進(jìn)制
QString str = ui->ba->text();//八進(jìn)制
bool ok;
int value = str.toInt(&ok, 8);//以八進(jìn)制數(shù)讀入
str = QString::number(value,10);//轉(zhuǎn)為十進(jìn)制
ui->shi->setText(str);
str = str.setNum(value,2);//轉(zhuǎn)為二進(jìn)制
ui->er->setText(str);
str = QString::number(value,16).toUpper();//轉(zhuǎn)為十六進(jìn)制
ui->shiliu->setText(QString("0x%1").arg(str));
}好啦,到這里,代碼就結(jié)束啦,是不是感覺很簡(jiǎn)單?!
最后附上源碼,親測(cè)可運(yùn)行,如果你在運(yùn)行時(shí),出現(xiàn)問題,可以留言。
.pro 文件源碼
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頭文件.h源碼
#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_Hmain.cpp源碼
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}.cpp源碼
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("各種進(jìn)制之間相互轉(zhuǎn)換");
}
MainWindow::~MainWindow()
{
delete ui;
}
//QString::number()和setNum()都可以轉(zhuǎn)換
void MainWindow::on_btn1_clicked()
{//十進(jìn)制轉(zhuǎn)為其他進(jìn)制
QString str = ui->shi->text();
int value = str.toInt();//十進(jìn)制,toInt()默認(rèn)是10進(jìn)制數(shù)
str = str.setNum(value,2);//轉(zhuǎn)為二進(jìn)制
ui->er->setText(str);
str = str.setNum(value,16).toUpper();//轉(zhuǎn)為十六進(jìn)制
ui->shiliu->setText(QString("0x%1").arg(str));
str = str.setNum(value,8);//轉(zhuǎn)為八進(jìn)制
ui->ba->setText(str);
}
void MainWindow::on_btn2_clicked()
{//二進(jìn)制轉(zhuǎn)為其他進(jìn)制
QString str = ui->er->text();//二進(jìn)制
bool ok;
int value = str.toInt(&ok, 2);//以二進(jìn)制數(shù)讀入,讀取成功ok=true;
qDebug() << "ok=" << ok;
str = QString::number(value,10);//轉(zhuǎn)為十進(jìn)制
ui->shi->setText(str);
str = QString::number(value,16).toUpper();//轉(zhuǎn)為十六進(jìn)制
ui->shiliu->setText(QString("0x%1").arg(str));
str = QString::number(value,8);//轉(zhuǎn)為八進(jìn)制
ui->ba->setText(str);
}
void MainWindow::on_btn3_clicked()
{//十六進(jìn)制轉(zhuǎn)為其他進(jìn)制
QString str = ui->shiliu->text();//十六進(jìn)制
bool ok;
int value = str.toInt(&ok, 16);//以十六進(jìn)制數(shù)讀入
str = QString::number(value,10);//轉(zhuǎn)為十進(jìn)制
ui->shi->setText(str);
str = str.setNum(value,2);//轉(zhuǎn)為二進(jìn)制
ui->er->setText(str);
str = QString::number(value,8);//轉(zhuǎn)為八進(jìn)制
ui->ba->setText(str);
}
void MainWindow::on_btn4_clicked()
{//八進(jìn)制轉(zhuǎn)為其他進(jìn)制
QString str = ui->ba->text();//八進(jìn)制
bool ok;
int value = str.toInt(&ok, 8);//以八進(jìn)制數(shù)讀入
str = QString::number(value,10);//轉(zhuǎn)為十進(jìn)制
ui->shi->setText(str);
str = str.setNum(value,2);//轉(zhuǎn)為二進(jìn)制
ui->er->setText(str);
str = QString::number(value,16).toUpper();//轉(zhuǎn)為十六進(jìn)制
ui->shiliu->setText(QString("0x%1").arg(str));
}運(yùn)行后的界面如下:

到此這篇關(guān)于QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)QT進(jìn)制轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Qt Design Studio創(chuàng)建工程的實(shí)現(xiàn)方法
Qt Design Studio它允許設(shè)計(jì)人員和開發(fā)人員使用通用的設(shè)計(jì)、開發(fā)、分析和調(diào)試工具在不同的開發(fā)平臺(tái)上共享一個(gè)項(xiàng)目,本文主要介紹了Qt Design Studio創(chuàng)建工程的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下2022-05-05
C++11新特性之右值引用與完美轉(zhuǎn)發(fā)詳解
C++11標(biāo)準(zhǔn)為C++引入右值引用語(yǔ)法的同時(shí),還解決了一個(gè)短板,即使用簡(jiǎn)單的方式即可在函數(shù)模板中實(shí)現(xiàn)參數(shù)的完美轉(zhuǎn)發(fā)。本文就來講講二者的應(yīng)用,需要的可以參考一下2022-09-09
QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)的即時(shí)通信實(shí)例
下面小編就為大家?guī)硪黄猀T網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)的即時(shí)通信實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
C語(yǔ)言字符串函數(shù)模擬實(shí)現(xiàn)流程介紹
字符串函數(shù)(String processing function)也叫字符串處理函數(shù),指的是編程語(yǔ)言中用來進(jìn)行字符串處理的函數(shù),如C,pascal,Visual以及LotusScript中進(jìn)行字符串拷貝,計(jì)算長(zhǎng)度,字符查找等的函數(shù)2022-09-09
C語(yǔ)言斷言函數(shù)assert()的學(xué)習(xí)筆記
在C語(yǔ)言庫(kù)函數(shù)中提供了一個(gè)輔助調(diào)試程序的小型庫(kù),它是由assert()宏組成,本文就詳細(xì)的介紹了一下如何使用,感興趣的可以了解一下2021-11-11
C++?JSON庫(kù)?nlohmann::basic_json::accept的用法解析
nlohmann::basic_json::accept 是 Nlohmann JSON 庫(kù)中的一個(gè)方法,它用于檢查一個(gè)字符串是否可以解析為有效的 JSON,這篇文章主要介紹了C++?JSON庫(kù)nlohmann::basic_json::accept的用法,需要的朋友可以參考下2023-06-06
C++學(xué)校運(yùn)動(dòng)會(huì)管理系統(tǒng)的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了C++如何實(shí)現(xiàn)學(xué)校運(yùn)動(dòng)會(huì)管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
詳解C++中的函數(shù)調(diào)用和下標(biāo)以及成員訪問運(yùn)算符的重載
這篇文章主要介紹了詳解C++中的函數(shù)調(diào)用和下標(biāo)以及成員訪問運(yùn)算符,講到了這些二元運(yùn)算符使用的語(yǔ)法及重載,需要的朋友可以參考下2016-01-01

