QT 进制转换小工具 二进制/十进制/十六进制/浮点数转换

野生小菜鸟一只,程序效果如下,希望大家多多支持。

1. 按table切换LineEdit;

2. 输入要计算的数据后,按回车执行,计算后的数据会显示在同一行的另一个控件中。

3. LIneEdit内加入了正则表达式使之只能输入特定的数。

format.h

#ifndef FORMAT_H
#define FORMAT_H
#include <QDialog>
namespace Ui {
class Format;
}
class Format : public QDialog
{
    Q_OBJECT
public:
    explicit Format(QWidget *parent = 0);
    ~Format();
private:
    Ui::Format *ui;
private slots:
    void twoTten();
    void twoThex();
    void tenTtwo();
    void tenThex();
    void hexTtwo();
    void hexTten();
    void floatThex();
    void hexTfloat();
private:
    QString binToDec(QString strBin);
    QString decTobin(QString strDec);
    QString decToHex(QString strDec);
    QString hexToDec(QString strHex);
};
#endif // FORMAT_H

format.cpp

#include "format.h"
#include "ui_format.h"
#include <QDebug>

Format::Format(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Format)
{
    ui->setupUi(this);
    this->setAutoFillBackground(true);
    this->setPalette(QPalette(QColor(162,200,169)));

    QRegExp regBinary("[0-1]*");
    QValidator *validator = new QRegExpValidator(regBinary,this);
    ui->two_ten1->setValidator(validator);
    ui->two_hex1->setValidator(validator);

    QRegExp regDecimal("[0-9]*");
    QValidator *validator2 = new QRegExpValidator(regDecimal,this);
    ui->ten_hex1->setValidator(validator2);
    ui->ten_two1->setValidator(validator2);

    QRegExp regHexadecimal("[a-fA-F0-9]*");
    QValidator *validator3 = new QRegExpValidator(regHexadecimal,this);
    ui->hex_ten1->setValidator(validator3);
    ui->hex_two1->setValidator(validator3);

    connect(ui->two_ten1,SIGNAL(returnPressed()),this,SLOT(twoTten()));
    connect(ui->two_hex1,SIGNAL(returnPressed()),this,SLOT(twoThex()));
    connect(ui->ten_hex1,SIGNAL(returnPressed()),this,SLOT(tenThex()));
    connect(ui->ten_two1,SIGNAL(returnPressed()),this,SLOT(tenTtwo()));
    connect(ui->hex_ten1,SIGNAL(returnPressed()),this,SLOT(hexTten()));
    connect(ui->hex_two1,SIGNAL(returnPressed()),this,SLOT(hexTtwo()));
    connect(ui->float_hex1,SIGNAL(returnPressed()),this,SLOT(floatThex()));
    connect(ui->hex_float1,SIGNAL(returnPressed()),this,SLOT(hexTfloat()));
}

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

void Format::twoTten(){  //二进制转十进制
    QString binary = ui->two_ten1->text();
    QString decimal = binToDec(binary);
    ui->ten_two1->setText(decimal);
}
void Format::twoThex(){  //二进制转十六进制
    QString binary = ui->two_hex1->text();
    QString hexadecimal = decToHex(binToDec(binary));
    ui->hex_two1->setText(hexadecimal);
}
void Format::tenThex(){  //十进制转十六进制
    QString decimal = ui->ten_hex1->text();
    QString hexadecimal = decToHex(decimal);
    ui->hex_ten1->setText(hexadecimal);
}
void Format::tenTtwo(){  //十进制转二进制
    QString decimal = ui->ten_two1->text();
    QString binary = decTobin(decimal);
    ui->two_ten1->setText(binary);
}
void Format::hexTten(){  //十六进制转十进制
    QString hexadecimal = ui->hex_ten1->text();
    QString decimal = hexToDec(hexadecimal);
    ui->ten_hex1->setText(decimal);
}
void Format::hexTtwo(){  //十六进制转二进制
    QString hexadecimal = ui->hex_two1->text();
    QString binary = decTobin(hexToDec(hexadecimal));
    ui->two_hex1->setText(binary);
}
void Format::floatThex(){
    QString strFloat = ui->float_hex1->text();
    float f = strFloat.toFloat();
    int i = *((int *)&f);
    QString float2 = QString("%1").arg(i,8,16,QLatin1Char('0'));
    QString step =float2;
    float2 = float2.right(4)+step.left(4);
    ui->hex_float1->setText(float2);
}
void Format::hexTfloat(){
    QString strHex = ui->hex_float1->text();
    QString step = strHex;
    strHex = strHex.right(4)+step.left(4);
    int c = hexToDec(strHex).toInt();
    float d = *(float*)&c;
    QString radiation = QString("%1").arg(d);
    ui->float_hex1->setText(radiation);
}

xijei.cpp

#include "format.h"
#include <QDebug>

QString Format::binToDec(QString strBin){  //二进制转十进制
    QString decimal;
    int nDec = 0,nLen;
    int i,j,k;
    nLen = strBin.length();
    for(i=0;i<nLen;i++){
        if(strBin[nLen-i-1]=="0")
            continue;
        else{
            k=1;
            for(j=0;j<i;j++)
                k=k*2;
            nDec+=k;
        }
    }
    decimal = QString::number(nDec);
    return decimal;
}
QString Format::decTobin(QString strDec){  //十进制转二进制
    int nDec = strDec.toInt();
    int nYushu, nShang;
    QString strBin, strTemp;
    //TCHAR buf[2];
    bool bContinue = true;
    while ( bContinue )
    {
        nYushu = nDec % 2;
        nShang = nDec / 2;
        strBin=QString::number(nYushu)+strBin; //qDebug()<<strBin;
        strTemp = strBin;
        //strBin.Format("%s%s", buf, strTemp);
        nDec = nShang;
        if ( nShang == 0 )
            bContinue = false;
    }
    int nTemp = strBin.length() % 4;
    switch(nTemp)
    {
    case 1:
        //strTemp.Format(_T("000%s"), strBin);
        strTemp = "000"+strBin;
        strBin = strTemp;
        break;
    case 2:
        //strTemp.Format(_T("00%s"), strBin);
        strTemp = "00"+strBin;
        strBin = strTemp;
        break;
    case 3:
        //strTemp.Format(_T("0%s"), strBin);
        strTemp = "0"+strBin;
        strBin = strTemp;
        break;
    default:
        break;
    }
    return strBin;
}
QString Format::decToHex(QString strDec){  //十进制转十六进制
    int hex = strDec.toInt();
    QString hex1 = QString("%1").arg(hex,8,16,QLatin1Char('0'));
    return hex1;
}
int hex2(unsigned char ch){          //十六进制转换工具
    if((ch >= '0') && (ch <= '9'))
        return ch-0x30;
    else if((ch >= 'A') && (ch <= 'F'))
        return ch-'A'+10;
    else if((ch >= 'a') && (ch <= 'f'))
        return ch-'a'+10;
    return 0;
}
QString Format::hexToDec(QString strHex){  //十六进制转十进制
    int i;
    int v = 0;
    for(i=0;i<strHex.length();i++)
    {
        v*=16;
        v+=hex2(strHex[i].toLatin1());
    }
    return QString::number(v);
}

format.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Format</class>
 <widget class="QDialog" name="Format">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>242</width>
    <height>207</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Format</string>
  </property>
  <layout class="QGridLayout" name="gridLayout_2">
   <item row="0" column="0">
    <layout class="QGridLayout" name="gridLayout">
     <item row="0" column="0">
      <widget class="QLabel" name="label">
       <property name="text">
        <string>二进制转十进制:</string>
       </property>
      </widget>
     </item>
     <item row="0" column="1">
      <widget class="QLabel" name="label_2">
       <property name="text">
        <string>十进制转二进制:</string>
       </property>
      </widget>
     </item>
     <item row="1" column="0" colspan="2">
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <widget class="QLineEdit" name="two_ten1"/>
       </item>
       <item>
        <widget class="QLineEdit" name="ten_two1">
         <property name="text">
          <string/>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item row="2" column="0">
      <widget class="QLabel" name="label_3">
       <property name="text">
        <string>二进制转十六进制:</string>
       </property>
      </widget>
     </item>
     <item row="2" column="1">
      <widget class="QLabel" name="label_4">
       <property name="text">
        <string>十六进制转二进制:</string>
       </property>
      </widget>
     </item>
     <item row="3" column="0" colspan="2">
      <layout class="QHBoxLayout" name="horizontalLayout_2">
       <item>
        <widget class="QLineEdit" name="two_hex1"/>
       </item>
       <item>
        <widget class="QLineEdit" name="hex_two1">
         <property name="text">
          <string/>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item row="4" column="0">
      <widget class="QLabel" name="label_5">
       <property name="text">
        <string>十进制转十六进制:</string>
       </property>
      </widget>
     </item>
     <item row="4" column="1">
      <widget class="QLabel" name="label_6">
       <property name="text">
        <string>十六进制转十进制:</string>
       </property>
      </widget>
     </item>
     <item row="5" column="0" colspan="2">
      <layout class="QHBoxLayout" name="horizontalLayout_3">
       <item>
        <widget class="QLineEdit" name="ten_hex1">
         <property name="text">
          <string/>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QLineEdit" name="hex_ten1">
         <property name="text">
          <string/>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item row="6" column="0">
      <widget class="QLabel" name="label_7">
       <property name="text">
        <string>float转十六进制:</string>
       </property>
      </widget>
     </item>
     <item row="6" column="1">
      <widget class="QLabel" name="label_8">
       <property name="text">
        <string>十六进制转float:</string>
       </property>
      </widget>
     </item>
     <item row="7" column="0" colspan="2">
      <layout class="QHBoxLayout" name="horizontalLayout_4">
       <item>
        <widget class="QLineEdit" name="float_hex1"/>
       </item>
       <item>
        <widget class="QLineEdit" name="hex_float1">
         <property name="text">
          <string/>
         </property>
        </widget>
       </item>
      </layout>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

功能上:基本功能是有了,但程序在计算范围上有些局限;

界面上:美观性不好,需要怎样调整和设计出好看的界面还未可知。

要是有路过的野生大牛还请对这个程序指点一二,不胜感激!

工具地址:

https://download.csdn.net/download/weixin_41493717/20685513

  • 13
    点赞
  • 84
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值