//文件夹是否存在
static bool FolderIsExist(QString strFolder)
{
QDir tempFolder(strFolder);
return tempFolder.exists();
}
//文件是否存在
static bool FileIsExist(QString strFile)
{
QFile tempFile(strFile);
return tempFile.exists();
}
//判断是否是IP地址
static bool isIpAddress(const QString &ip)
{
QRegExp regExp("((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)");
return regExp.exactMatch(ip);
}
//16进制字符串转字节数组
static QByteArray HexStrToByteArray(QString str)
{
QByteArray senddata;
int hexdata, lowhexdata;
int hexdatalen = 0;
int len = str.length();
senddata.resize(len/2);
char lstr;//, hstr;
for(int i=0; i<len; )
{
char hstr = str[i].toLatin1();
if(hstr == ' ')
{
i++;
continue;
}
i++;
if(i >= len)
break;
lstr = str[i].toLatin1();
hexdata = ConvertHexChar(hstr);
lowhexdata = ConvertHexChar(lstr);
if((hexdata == 16) || (lowhexdata == 16))
break;
else
hexdata = hexdata*16 + lowhexdata;
i++;
senddata[hexdatalen] = static_cast<char>(hexdata);
hexdatalen++;
}
senddata.resize(hexdatalen);
return senddata;
}
//Conver字符型
static char ConvertHexChar(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;
else
return (-1);
}
//字节数组转16进制字符串
static QString ByteArrayToHexStr(QByteArray data)
{
QString temp = "";
QString hex = data.toHex();
for (int i=0; i<hex.length(); i=i+2)
{
temp += hex.mid(i, 2) + " ";
}
return temp.trimmed().toUpper();
}
// 16进制字符串转10进制
static int StrHexToDecimal(QString strHex)
{
bool ok;
return strHex.toInt(&ok, 16);
}
// 10进制字符串转10进制
static int StrDecimalToDecimal(QString strDecimal)
{
bool ok;
return strDecimal.toInt(&ok, 10);
}
// 2进制字符串转10进制
static int StrBinToDecimal(QString strBin)
{
bool ok;
return strBin.toInt(&ok, 2);
}
//16进制字符串转2进制字符串
static QString StrHexToStrBin(QString strHex)
{
uchar decimal = static_cast<uchar>(StrHexToDecimal(strHex));
QString bin = QString::number(decimal, 2);
uchar len = static_cast<uchar>(bin.length());
if (len < 8)
{
for (int i=0; i<8-len; i++)
{
bin = "0" + bin;
}
}
return bin;
}
//10进制转2进制字符串一个字节
static QString DecimalToStrBin1(int decimal)
{
QString bin = QString::number(decimal, 2);
uchar len = static_cast<uchar>(bin.length());
if (len <= 8)
{
for (int i=0; i<8-len; i++)
{
bin = "0" + bin;
}
}
return bin;
}
//10进制转2进制字符串两个字节
static QString DecimalToStrBin2(int decimal)
{
QString bin = QString::number(decimal,2);
uchar len = static_cast<uchar>(bin.length());
if (len <= 16)
{
for (int i=0; i<16-len; i++)
{
bin = "0" + bin;
}
}
return bin;
}
//计算校验码
static uchar GetCheckCode(uchar data[], uchar len)
{
uchar temp = 0;
for (uchar i=0; i<len; i++)
{
temp += data[i];
}
return temp%256;
}
//将溢出的char转为正确的uchar
static uchar GetUChar(char data)
{
uchar temp;
if (data > 126)
{
temp = static_cast<uchar>(data + 256);
}
else
{
temp = static_cast<uchar>(data);
}
return temp;
}
//延时
static void Sleep(int sec)
{
QTime dieTime = QTime::currentTime().addMSecs(sec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
//获取当前路径
static QString GetCurrentPath()
{
return QString(QCoreApplication::applicationDirPath() + "/");
}