124 lines
3.2 KiB
C++
124 lines
3.2 KiB
C++
#include "util.h"
|
||
#include <QMessageBox>
|
||
#include <QWidget>
|
||
#include <QTime>
|
||
#include <QEventLoop>
|
||
#include <QCoreApplication>
|
||
#include <sstream>
|
||
#include <cmath>
|
||
#include <QBuffer>
|
||
#include <QFile>
|
||
#include <qrandom.h>
|
||
#include <QTextStream>
|
||
using namespace std;
|
||
Util::Util()
|
||
{
|
||
|
||
}
|
||
|
||
void Util::WriteLog(string logInfo)
|
||
{
|
||
Util::Assert(false);
|
||
}
|
||
|
||
void Util::MessageBox(QWidget *parentWnd, std::string msg, std::string title /*= "信息"*/)
|
||
{
|
||
QMessageBox::information(parentWnd, ToQstr(title), ToQstr(msg), QMessageBox::Ok, QMessageBox::NoButton);
|
||
|
||
}
|
||
|
||
QString Util::ToQstr(std::string str)
|
||
{
|
||
return QString::fromStdString(str);
|
||
}
|
||
|
||
string Util::ToStdStr(QString str /*= "默认的参数:你好2 helloworld *&……%¥#@!"*/)
|
||
{
|
||
|
||
return string(str.toLocal8Bit());
|
||
}
|
||
|
||
bool Util::IsEqual(double a, double b) //比较两个浮点数使否相等
|
||
{
|
||
return fabs(a - b) <= EPSLON;
|
||
}
|
||
|
||
void Util::delay(int ms) //延迟指定的ms毫秒数
|
||
{
|
||
QTime dieTime = QTime::currentTime().addMSecs(ms);
|
||
while (QTime::currentTime() < dieTime)
|
||
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
||
}
|
||
|
||
|
||
QString Util::GetId()
|
||
{
|
||
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
|
||
QDateTime currentTime = QDateTime::currentDateTime();
|
||
QString currentTimeStr = currentTime.toString("yyyyMMddhhmmss");
|
||
|
||
int n = qrand() % 10;
|
||
//QString random = QString("%1").arg(n,6,10,QLatin1Char('0'));
|
||
|
||
return currentTimeStr + QString::number(n);
|
||
}
|
||
|
||
//i要转化的十进制整数,width转化后的宽度,位数不足则补0
|
||
std::string Util::Dec2Hex(int i, int width)
|
||
{
|
||
std::stringstream ioss; //定义字符串流
|
||
std::string s_temp; //存放转化后字符
|
||
ioss << std::hex << i; //以十六制形式输出
|
||
ioss >> s_temp;
|
||
|
||
if(width > s_temp.size())
|
||
{
|
||
std::string s_0(width - s_temp.size(), '0'); //位数不够则补0
|
||
s_temp = s_0 + s_temp; //合并
|
||
}
|
||
|
||
std::string s = s_temp.substr(s_temp.length() - width, s_temp.length()); //取右width位
|
||
transform(s.begin(), s.end(), s.begin(), ::toupper);
|
||
|
||
return s;
|
||
}
|
||
|
||
|
||
|
||
QString Util::Img2base64(QImage& image){
|
||
// 将 QImage 写入 QByteArray
|
||
QByteArray byteArray;
|
||
QBuffer buffer(&byteArray);
|
||
buffer.open(QIODevice::WriteOnly);
|
||
image.save(&buffer, "JPEG"); // 保存为 PNG 格式,您也可以选择其他格式
|
||
buffer.close();
|
||
|
||
// 将 QByteArray 转换为 Base64 编码的字符串
|
||
QString base64Str = byteArray.toBase64();
|
||
return base64Str;
|
||
}
|
||
|
||
std::string Util::Byte2Hex(unsigned char* bytes, int bytelength)
|
||
{
|
||
string str("");
|
||
string str2("0123456789abcdef");
|
||
for (int i=0;i<bytelength;i++) {
|
||
int b;
|
||
b = 0x0f&(bytes[i]>>4);
|
||
char s1 = str2.at(b);
|
||
str.append(1,str2.at(b));
|
||
b = 0x0f & bytes[i];
|
||
str.append(1,str2.at(b));
|
||
char s2 = str2.at(b);
|
||
}
|
||
return str;
|
||
}
|
||
|
||
// 生成11位随机数
|
||
qint64 Util::generate11DigitRandomNumber() {
|
||
qint64 min = 10000000000LL; // 11位最小值
|
||
qint64 max = 99999999999LL; // 11位最大值
|
||
qint64 range = max - min + 1;
|
||
qint64 randNum = QRandomGenerator::global()->generate64() % range + min;
|
||
return randNum;
|
||
} |