二进制、十进制、十六进制间的相互转换
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
// 计算总价
void Widget::on_btnTotal_clicked()
{
bool ok = false;
int num = ui->editNum->text().toInt(&ok, 10);
if(!ok){
qDebug("read num failed \n");
return;
}
float price = ui->editPrice->text().toFloat(&ok);
if(!ok){
qDebug("read price failed \n");
return;
}
float total = price * num;
QString str = QString::number(total, 'f', 2);
ui->editTotal->setText(str);
}
// qDebug测试
void Widget::on_btnDebug_clicked()
{
qDebug("PI = %f", 3.14159265);
QString str = QString::number(3.14159265, 'f', 10);
qDebug("PI = %s", str.toLocal8Bit().data());
}
// DEC --> 其他进制
void Widget::on_btnDecToOther_clicked()
{
bool ok = false;
int val = ui->editDec->text().toInt(&ok, 10);
if(!ok){
qDebug("DEC --> other failed \n");
return;
}
QString strHex = QString::number(val, 16);
ui->editHex->setText(strHex);
QString strBin = QString::number(val, 2);
ui->editBin->setText(strBin);
}
// BIN --> 其他进制
void Widget::on_btnBinToOther_clicked()
{
bool ok = false;
int val = ui->editBin->text().toInt(&ok, 2);
if(!ok){
qDebug("BIN --> other failed \n");
return;
}
QString strHex = QString::number(val, 16);
ui->editHex->setText(strHex);
QString strDec = QString::number(val, 10);
ui->editDec->setText(strDec);
}
// HEX --> 其他进制
void Widget::on_btnHexToOther_clicked()
{
bool ok = false;
int val = ui->editHex->text().toInt(&ok, 16);
if(!ok){
qDebug("HEX --> other failed \n");
return;
}
QString strBin = QString::number(val, 2);
ui->editBin->setText(strBin);
QString strDec = QString::number(val, 10);
ui->editDec->setText(strDec);
}