| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #include "Utils.h"
- #include "CommWrapperBase.h"
- #include <algorithm>
- #include <sstream>
- #include <iomanip>
- namespace Utils
- {
- // 字符串转字节数组
- std::vector<BYTE> 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<BYTE> 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<BYTE>(std::stoul(byte_str, nullptr, 16));
- bytes.push_back(byte_val);
- }
- return bytes;
- }
- std::string Utils::ConvertBytesToHexString(const std::vector<BYTE> &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<int>(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
|