#ifndef _UTILS_TIME_BYTE_UTIL_H_ #define _UTILS_TIME_BYTE_UTIL_H_ #include #include #include /** * @brief 字节操作类 */ class ByteUtil { public: static void copyUnsignedCharArray(unsigned char* source, unsigned char destination[], size_t size) { std::copy(source, source + size, destination); } static void copyUnsignedCharArray(unsigned char* source, unsigned char* destination, size_t size, size_t& offset) { // 确保不超出目标数组的界限 size_t remainingSize = offset + size > 1024 ? 1024 - offset : size; if (remainingSize > 0) { std::copy(source, source + remainingSize, destination + offset); offset += remainingSize; // 更新偏移量 } } // std::string byteToString(const unsigned char* bytes, unsigned long int length) { // return std::string(reinterpret_cast(bytes), length); // } static std::string byteToString(const unsigned char* data, size_t size) { std::string result; // 将每个 unsigned char 转换为 char 并添加到结果字符串中 for (size_t i = 0; i < size; ++i) { result += static_cast(data[i]); } return result; } static std::string convertToString(const unsigned char* data, size_t size) { std::string dataStr = ""; for (int i = 0; i < size; ++i) { char buf[1024]; sprintf(buf, "%02x", data[i]); dataStr += buf; } return dataStr; } static unsigned char stringToByte(const std::string& str) { std::stringstream ss; ss << std::hex << str; unsigned int intValue; ss >> intValue; return static_cast(intValue); } static std::string timestampToHex(int timestamp) { std::stringstream ss; ss << std::hex << timestamp; return ss.str(); } static std::string timestampToHex(time_t timestamp) { std::stringstream ss; ss << std::hex << timestamp; return ss.str(); } }; #endif /* _UTILS_TIME_BYTE_UTIL_H_ */