函数名

//判断是否是IP地址
static bool isIP(const QString &ip);
//判断是否是MAC地址
static bool isMac(const QString &mac);
//判断是否是合法的电话号码
static bool isTel(const QString &tel);
//判断是否是合法的邮箱地址
static bool isEmail(const QString &email);

//16进制字符串转10进制
static int strHexToDecimal(const QString &strHex);
//10进制字符串转10进制
static int strDecimalToDecimal(const QString &strDecimal);
//2进制字符串转10进制
static int strBinToDecimal(const QString &strBin);

//16进制字符串转2进制字符串
static QString strHexToStrBin(const QString &strHex);
//10进制转2进制字符串一个字节
static QString decimalToStrBin1(int decimal);
//10进制转2进制字符串两个字节
static QString decimalToStrBin2(int decimal);
//10进制转16进制字符串,补零.
static QString decimalToStrHex(int decimal);

函数体

bool QUIHelper::isIP(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);
}

bool QUIHelper::isMac(const QString &mac)
{
QRegExp RegExp("^[A-F0-9]{2}(-[A-F0-9]{2}){5}$");
return RegExp.exactMatch(mac);
}

bool QUIHelper::isTel(const QString &tel)
{
if (tel.length() != 11) {
return false;
}

if (!tel.startsWith("13") && !tel.startsWith("14") && !tel.startsWith("15") && !tel.startsWith("18")) {
return false;
}

return true;
}

bool QUIHelper::isEmail(const QString &email)
{
if (!email.contains("@") || !email.contains(".com")) {
return false;
}

return true;
}

int QUIHelper::strHexToDecimal(const QString &strHex)
{
bool ok;
return strHex.toInt(&ok, 16);
}

int QUIHelper::strDecimalToDecimal(const QString &strDecimal)
{
bool ok;
return strDecimal.toInt(&ok, 10);
}

int QUIHelper::strBinToDecimal(const QString &strBin)
{
bool ok;
return strBin.toInt(&ok, 2);
}

QString QUIHelper::strHexToStrBin(const QString &strHex)
{
uchar decimal = strHexToDecimal(strHex);
QString bin = QString::number(decimal, 2);
uchar len = bin.length();

if (len < 8) {
for (int i = 0; i < 8 - len; i++) {
bin = "0" + bin;
}
}

return bin;
}

QString QUIHelper::decimalToStrBin1(int decimal)
{
QString bin = QString::number(decimal, 2);
uchar len = bin.length();

if (len <= 8) {
for (int i = 0; i < 8 - len; i++) {
bin = "0" + bin;
}
}

return bin;
}

QString QUIHelper::decimalToStrBin2(int decimal)
{
QString bin = QString::number(decimal, 2);
uchar len = bin.length();

if (len <= 16) {
for (int i = 0; i < 16 - len; i++) {
bin = "0" + bin;
}
}

return bin;
}

QString QUIHelper::decimalToStrHex(int decimal)
{
QString temp = QString::number(decimal, 16);
if (temp.length() == 1) {
temp = "0" + temp;
}

return temp;
}