You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

537 lines
13 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "Utils.h"
#include <string> // std::string
#include <sstream> // std::stringstream
#include <fstream> // std::ifstream/ofstream
#include <algorithm>
#include <iostream>
#include <QTime>
#include <QCoreApplication>
#include <fstream>
#include <algorithm>
#include <iostream>
#include <QDebug>
std::string Utils::m_hexTable = "0123456789ABCDEF";
// 字符转16进制字符串不适用于汉字字符
//std::string Utils::charToHexStr(unsigned char ch)
//{
// const std::string hex = "0123456789ABCDEF";
// std::stringstream ss;
// ss << hex[ch >> 4] << hex[ch & 0xf];
// return ss.str();
//}
// 字符串转十六进制字符串,可用于汉字字符串
// str : 待转换的字符串
// separator : 十六进制分隔符
//std::string Utils::strToHexStr(std::string str, std::string separator)
//{
// const std::string hex = "0123456789ABCDEF";
// std::stringstream ss;
//// int nSize = str.size();
//// int nLength = str.size();
// for (std::string::size_type i = 0; i < str.size(); ++i)
// {
// ss << hex[(unsigned char)str[i] >> 4] << hex[(unsigned char)str[i] & 0xf] << separator;
// }
// return ss.str();
//}
// 字符转字符流
// char为1字节按字节将数据存储到字符串中
std::string Utils::charToByteStr(unsigned char val)
{
std::string s;
s.append((char*)&val, 1);
return s;
}
// 短整型转字符流
// short为2字节按字节将数据存储到字符串中
std::string Utils::shortToByteStr(unsigned short val)
{
std::string s;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
s.append((char *)&val + 1, 1);
s.append((char *)&val + 0, 1);
#else
s.append((char *)&val + 0, 1);
s.append((char *)&val + 1, 1);
#endif
return s;
}
// 整型转字符流
// int为4字节按字节将数据存储到字符串中
std::string Utils::intToByteStr(unsigned int val)
{
std::string s;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
s.append((char *)&val + 3, 1);
s.append((char *)&val + 2, 1);
s.append((char *)&val + 1, 1);
s.append((char *)&val + 0, 1);
#else
s.append((char *)&val + 0, 1);
s.append((char *)&val + 1, 1);
s.append((char *)&val + 2, 1);
s.append((char *)&val + 3, 1);
#endif
return s;
}
// byte字符串转字符
unsigned char Utils::byteStrToChar(std::string val)
{
return (unsigned char)val.c_str()[0];
}
// byte字符串转短整型
unsigned short Utils::byteStrToShort(std::string val)
{
unsigned short us = 0;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
us = ((unsigned short)val.c_str()[0] << 8 & 0xFF00) + ((unsigned short)val.c_str()[1] << 0 & 0x00FF);
#else
us = ((unsigned short)val.c_str()[0] << 0 & 0x00FF) + ((unsigned short)val.c_str()[1] << 8 & 0xFF00);
#endif
return us;
}
// c1,c2为按顺序的两个字节
unsigned short Utils::charToShort(unsigned char c1, unsigned char c2)
{
unsigned short us = 0;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
us = ((unsigned short)c1 << 8 & 0xFF00) + ((unsigned short)c2 << 0 & 0x00FF);
#else
us = ((unsigned short)c1 << 0 & 0x00FF) + ((unsigned short)c2 << 8 & 0xFF00);
#endif
return us;
}
// byte字符串转整形
unsigned int Utils::byteStrToInt(std::string val)
{
unsigned int ui = 0;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
// unsigned int a = (unsigned int)val.c_str()[0] << 24;
// unsigned int b = (unsigned int)val.c_str()[1] << 16;
// unsigned int c = (unsigned int)val.c_str()[2] << 8;
// unsigned int d = (unsigned int)val.c_str()[3] << 0;
ui = ((unsigned int)val.c_str()[0] << 24 & 0xFF000000) +
((unsigned int)val.c_str()[1] << 16 & 0x00FF0000) +
((unsigned int)val.c_str()[2] << 8 & 0x0000FF00) +
((unsigned int)val.c_str()[3] << 0 & 0x000000FF) ;
#else
ui = ((unsigned int)val.c_str()[0] << 0 & 0x000000FF) +
((unsigned int)val.c_str()[1] << 8 & 0x0000FF00) +
((unsigned int)val.c_str()[2] << 16 & 0x00FF0000) +
((unsigned int)val.c_str()[3] << 24 & 0xFF000000);
#endif
return ui;
}
//std::string Utils::charToHexStr(const unsigned char& c)
//{
// std::string hexStr;
// hexStr.reserve(2);
// hexStr.push_back(m_hexTable.at((c >> 4) & 0x0F));
// hexStr.push_back(m_hexTable.at((c >> 0) & 0x0F));
// return hexStr;
//}
//std::string Utils::ucharToHexStr(const unsigned char& c)
//{
// static const char* lut = "0123456789ABCDEF";
// std::string hexStr;
// hexStr.reserve(2);
// hexStr.push_back(lut[(c >> 4) & 0x0F]);
// hexStr.push_back(lut[(c >> 0) & 0x0F]);
// return hexStr;
//}
std::string Utils::strToHexStr(const std::string& str)
{
static const char* lut = "0123456789ABCDEF";
std::string hexStr;
size_t len = str.length();
hexStr.reserve(2 * len);
for(size_t i=0; i<len; i++)
{
const unsigned c = str[i];
hexStr.push_back(lut[(c >> 4) & 0x0F]);
hexStr.push_back(lut[(c >> 0) & 0x0F]);
}
return hexStr;
}
std::string Utils::hexStrToStr(const std::string& hexStr)
{
static const char* const lut = "0123456789ABCDEF";
size_t len = hexStr.length();
if (len & 1)
{
throw std::invalid_argument("odd length");
}
std::string str;
str.reserve(len / 2);
for (size_t i = 0; i < len; i += 2)
{
char a = hexStr[i];
const char* p = std::lower_bound(lut, lut + 16, a);
if (*p != a)
{
throw std::invalid_argument("not a hex digit");
}
char b = hexStr[i + 1];
const char* q = std::lower_bound(lut, lut + 16, b);
if (*q != b)
{
throw std::invalid_argument("not a hex digit");
}
str.push_back(((char)(p - lut) << 4) | (char)(q - lut));
}
return str;
}
std::string Utils::ucharToStr(const unsigned char& val)
{
std::string s;
s.append((char *)&val, 1);
return s;
}
std::string Utils::ushortToStr(const unsigned short& val)
{
std::string s;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
s.append((char *)&val + 1, 1);
s.append((char *)&val + 0, 1);
#else
s.append((char *)&val + 0, 1);
s.append((char *)&val + 1, 1);
#endif
return s;
}
std::string Utils::uintToStr(const unsigned int& val)
{
std::string s;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
s.append((char *)&val + 3, 1);
s.append((char *)&val + 2, 1);
s.append((char *)&val + 1, 1);
s.append((char *)&val + 0, 1);
#else
s.append((char *)&val + 0, 1);
s.append((char *)&val + 1, 1);
s.append((char *)&val + 2, 1);
s.append((char *)&val + 3, 1);
#endif
return s;
}
std::string Utils::ulongToStr(const unsigned long& val)
{
std::string s;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
s.append((char *)&val + 3, 1);
s.append((char *)&val + 2, 1);
s.append((char *)&val + 1, 1);
s.append((char *)&val + 0, 1);
#else
s.append((char *)&val + 0, 1);
s.append((char *)&val + 1, 1);
s.append((char *)&val + 2, 1);
s.append((char *)&val + 3, 1);
#endif
return s;
}
std::string Utils::charToStr(const char& val)
{
std::string s;
s.append(&val, 1);
return s;
}
std::string Utils::shortToStr(const short& val)
{
std::string s;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
s.append((char *)&val + 1, 1);
s.append((char *)&val + 0, 1);
#else
s.append((char *)&val + 0, 1);
s.append((char *)&val + 1, 1);
#endif
return s;
}
std::string Utils::intToStr(const int& val)
{
std::string s;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
s.append((char *)&val + 3, 1);
s.append((char *)&val + 2, 1);
s.append((char *)&val + 1, 1);
s.append((char *)&val + 0, 1);
#else
s.append((char *)&val + 0, 1);
s.append((char *)&val + 1, 1);
s.append((char *)&val + 2, 1);
s.append((char *)&val + 3, 1);
#endif
return s;
}
std::string Utils::longToStr(const long& val)
{
std::string s;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
s.append((char *)&val + 3, 1);
s.append((char *)&val + 2, 1);
s.append((char *)&val + 1, 1);
s.append((char *)&val + 0, 1);
#else
s.append((char *)&val + 0, 1);
s.append((char *)&val + 1, 1);
s.append((char *)&val + 2, 1);
s.append((char *)&val + 3, 1);
#endif
return s;
}
unsigned char Utils::strToUchar(const std::string& str)
{
unsigned char val;
val = str.at(0);
return val;
}
unsigned short Utils::strToUshort(const std::string& str)
{
unsigned short val;
if(str.length() < sizeof(unsigned short))
{
throw std::invalid_argument("length not enough");
}
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
((char *)&val)[0] = str.at(1);
((char *)&val)[1] = str.at(0);
#else
((char *)&val)[0] = str.at(0);
((char *)&val)[1] = str.at(1);
#endif
return val;
}
unsigned int Utils::strToUint(const std::string& str)
{
unsigned int val;
if(str.length() < sizeof(unsigned int))
{
throw std::invalid_argument("length not enough");
}
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
((char *)&val)[0] = str.at(3);
((char *)&val)[1] = str.at(2);
((char *)&val)[2] = str.at(1);
((char *)&val)[3] = str.at(0);
#else
((char *)&val)[0] = str.at(0);
((char *)&val)[1] = str.at(1);
((char *)&val)[2] = str.at(2);
((char *)&val)[3] = str.at(3);
#endif
return val;
}
unsigned long Utils::strToUlong(const std::string& str)
{
unsigned long val;
if(str.length() < sizeof(unsigned long))
{
throw std::invalid_argument("length not enough");
}
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
((char *)&val)[0] = str.at(3);
((char *)&val)[1] = str.at(2);
((char *)&val)[2] = str.at(1);
((char *)&val)[3] = str.at(0);
#else
((char *)&val)[0] = str.at(0);
((char *)&val)[1] = str.at(1);
((char *)&val)[2] = str.at(2);
((char *)&val)[3] = str.at(3);
#endif
return val;
}
char Utils::strToChar(const std::string& str)
{
char val;
val = str.at(0);
return val;
}
short Utils::strToShort(const std::string& str)
{
short val;
if(str.length() < sizeof(short))
{
throw std::invalid_argument("length not enough");
}
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
((char *)&val)[0] = str.at(1);
((char *)&val)[1] = str.at(0);
#else
((char *)&val)[0] = str.at(0);
((char *)&val)[1] = str.at(1);
#endif
return val;
}
int Utils::strToInt(const std::string& str)
{
int val;
if(str.length() < sizeof(int))
{
throw std::invalid_argument("length not enough");
}
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
((char *)&val)[0] = str.at(3);
((char *)&val)[1] = str.at(2);
((char *)&val)[2] = str.at(1);
((char *)&val)[3] = str.at(0);
#else
((char *)&val)[0] = str.at(0);
((char *)&val)[1] = str.at(1);
((char *)&val)[2] = str.at(2);
((char *)&val)[3] = str.at(3);
#endif
return val;
}
long Utils::strToLong(const std::string& str)
{
long val;
if(str.length() < sizeof(long))
{
throw std::invalid_argument("length not enough");
}
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
((char *)&val)[0] = str.at(3);
((char *)&val)[1] = str.at(2);
((char *)&val)[2] = str.at(1);
((char *)&val)[3] = str.at(0);
#else
((char *)&val)[0] = str.at(0);
((char *)&val)[1] = str.at(1);
((char *)&val)[2] = str.at(2);
((char *)&val)[3] = str.at(3);
#endif
return val;
}
void Utils::testTransOfTypeAndStr()
{
unsigned char ucA = 0x12;
std::string sucB = Utils::ucharToStr(ucA);
unsigned char ucC = Utils::strToUchar(sucB);
qDebug() << "testTransOfTypeAndStr : uchar -> " << (ucA == ucC);
unsigned short usA = 0x1234;
std::string susB = Utils::ushortToStr(usA);
unsigned short usC = Utils::strToUshort(susB);
qDebug() << "testTransOfTypeAndStr : ushort -> " << (usA == usC);
unsigned int uiA = 0x12345678;
std::string suiB = Utils::uintToStr(uiA);
unsigned int uiC = Utils::strToUint(suiB);
qDebug() << "testTransOfTypeAndStr : uint -> " << (uiA == uiC);
unsigned long ulA = 0x87654321;
std::string sulB = Utils::uintToStr(ulA);
unsigned long ulC = Utils::strToUint(sulB);
qDebug() << "testTransOfTypeAndStr : ulong -> " << (ulA == ulC);
}
// 判断字节序是大端序还是小端序,默认小端序
// 对于int a = 1如果a的内存分布为
// 00 00 00 01 则为大端序
// 01 00 00 00 则为小端序
// 低地址---->高地址
int Utils::byteOrder()
{
union
{
short value;
char union_bytes[sizeof(short)];
} test;
test.value=0x0102;
if((test.union_bytes[0] == 1) && (test.union_bytes[1] == 2))
{
return BIG_ENDIA;
}
else if((test.union_bytes[0] == 2) && (test.union_bytes[1] == 1))
{
return LITTLE_ENDIA;
}
return LITTLE_ENDIA;
}
//
// 不阻塞的延时
//
void Utils::Sleep(int msec)
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
std::string Utils::readFile(const std::string& fileName)
{
std::ifstream ifile;
std::string str;
ifile.open(fileName);
ifile >> str;
ifile.close();
return str;
}
void Utils::writeFile(const std::string& fileName, const std::string& content)
{
std::ofstream ofile;
ofile.open(fileName);
ofile << content;
ofile.close();
}