

#ifndef _UTILS_TIME_BYTE_UTIL_H_
#define _UTILS_TIME_BYTE_UTIL_H_

#include <string>
#include <iostream>
#include <sstream>


/**
 * @brief 字节操作类
 */
class ByteUtil {
public:
	static void copyUnsignedCharArray(unsigned char* source, unsigned char destination[], size_t size) {
	    std::copy(source, source + size, destination);
	}

//	std::string byteToString(const unsigned char* bytes, unsigned long int length) {
//	    return std::string(reinterpret_cast<const char*>(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<char>(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<unsigned char>(intValue);
	}

	// 将十六进制字符串转换为无符号字符
	static unsigned char hexStrToByte_16(const std::string& hexStr) {
	    // 使用 std::stol 转换字符串为长整数
	    long byte = std::stol(hexStr, nullptr, 16);
	    // 确保转换后的值在 unsigned char 的范围内
	    if (byte < 0 || byte > std::numeric_limits<unsigned char>::max()) {
	        throw std::out_of_range("Hex string value out of unsigned char range");
	    }
	    // 将长整型转换为 unsigned char 并返回
	    return static_cast<unsigned char>(byte);
	}


	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_ */
