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.

101 lines
4.3 KiB
C

1 year ago
#pragma once
#include <string>
#define LITTLE_ENDIA 0 // 小端字节序
#define BIG_ENDIA 1 // 大端字节序
class Utils
{
public:
Utils(){};
virtual ~Utils(){};
// 字符转十六进制
//static std::string charToHexStr(unsigned char ch);
// 字符串转十六进制
//static std::string strToHexStr(std::string str, std::string separator = "");
// 字符转byte字符串
static std::string charToByteStr(unsigned char val);
// 短整型转byte字符串
static std::string shortToByteStr(unsigned short val);
// 整型转byte字符串
static std::string intToByteStr(unsigned int val);
// byte字符串转字符
static unsigned char byteStrToChar(std::string val);
// byte字符串转短整型
static unsigned short byteStrToShort(std::string val);
// byte字符串转整形
static unsigned int byteStrToInt(std::string val);
static unsigned short charToShort(unsigned char c1, unsigned char c2);
//static std::string ucharToHexStr(const unsigned char& c);
static std::string strToHexStr(const std::string& str);
static std::string hexStrToStr(const std::string& hexStr);
static std::string ucharToStr(const unsigned char& val);
static std::string ushortToStr(const unsigned short& val);
static std::string uintToStr(const unsigned int& val);
static std::string ulongToStr(const unsigned long& val);
static std::string charToStr(const char& val);
static std::string shortToStr(const short& val);
static std::string intToStr(const int& val);
static std::string longToStr(const long& val);
static std::string ucharToHexStr(const unsigned char& val) { return Utils::strToHexStr(Utils::ucharToStr(val)); }
static std::string ushortToHexStr(const unsigned short& val) { return Utils::strToHexStr(Utils::ushortToStr(val)); }
static std::string uintToHexStr(const unsigned int& val) { return Utils::strToHexStr(Utils::uintToStr(val)); }
static std::string ulongToHexStr(const unsigned long& val) { return Utils::strToHexStr(Utils::ulongToStr(val)); }
static std::string charToHexStr(const char& val) { return Utils::strToHexStr(Utils::charToStr(val)); }
static std::string shortToHexStr(const short& val) { return Utils::strToHexStr(Utils::shortToStr(val)); }
static std::string intToHexStr(const int& val) { return Utils::strToHexStr(Utils::intToStr(val)); }
static std::string longToHexStr(const long& val) { return Utils::strToHexStr(Utils::longToStr(val)); }
static unsigned char strToUchar(const std::string& str);
static unsigned short strToUshort(const std::string& str);
static unsigned int strToUint(const std::string& str);
static unsigned long strToUlong(const std::string& str);
static char strToChar(const std::string& str);
static short strToShort(const std::string& str);
static int strToInt(const std::string& str);
static long strToLong(const std::string& str);
static unsigned char hexStrToUchar(const std::string& str) { return Utils::strToUchar(Utils::hexStrToStr(str)); }
static unsigned short hexStrToUshort(const std::string& str) { return Utils::strToUshort(Utils::hexStrToStr(str)); }
static unsigned int hexStrToUint(const std::string& str) { return Utils::strToUint(Utils::hexStrToStr(str)); }
static unsigned long hexStrToUlong(const std::string& str) { return Utils::strToUlong(Utils::hexStrToStr(str)); }
static char hexStrToChar(const std::string& str) { return Utils::strToChar(Utils::hexStrToStr(str)); }
static short hexStrToShort(const std::string& str) { return Utils::strToShort(Utils::hexStrToStr(str)); }
static int hexStrToInt(const std::string& str) { return Utils::strToInt(Utils::hexStrToStr(str)); }
static long hexStrToLong(const std::string& str) { return Utils::strToLong(Utils::hexStrToStr(str)); }
static void testTransOfTypeAndStr();
// 检查字节序
static int byteOrder();
static void Sleep(int msec);
static std::string readFile(const std::string& fileName);
static void writeFile(const std::string& fileName, const std::string& content);
// 获取char字节的某一位数据 7 6 5 4 3 2 1 0
static unsigned char charBit(unsigned char c, unsigned char pos) { return ((c >> pos) & 0x01); }
private:
static std::string m_hexTable;
};