#include "Utils.h" #include "CommWrapperBase.h" #include #include #include namespace Utils { // 字符串转字节数组 std::vector ConvertHexStringToBytes(const std::string &hex_string) { std::string clean_hex = hex_string; // 移除空格 clean_hex.erase(std::remove(clean_hex.begin(), clean_hex.end(), ' '), clean_hex.end()); // 如果长度为奇数,补0 if (clean_hex.length() % 2 != 0) { clean_hex += '0'; } std::vector bytes; for (size_t i = 0; i < clean_hex.length(); i += 2) { std::string byte_str = clean_hex.substr(i, 2); BYTE byte_val = static_cast(std::stoul(byte_str, nullptr, 16)); bytes.push_back(byte_val); } return bytes; } std::string Utils::ConvertBytesToHexString(const std::vector &bytes) { std::stringstream ss; for (size_t i = 0; i < bytes.size(); i++) { if (i > 0) ss << " "; ss << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast(bytes[i]); } return ss.str(); } // 获取当前可执行文件目录 std::string Utils::GetExecutableDirectory() { char buffer[MAX_PATH]; GetModuleFileNameA(NULL, buffer, MAX_PATH); std::string::size_type pos = std::string(buffer).find_last_of("\\"); return std::string(buffer).substr(0, pos); } } // namespace Utils