gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
How to convert in c++ string like this "55 01 06 00 00 00 00 01 1A 0F 00 00 00 00 00 00…
- User
How to convert in c++ string like this "55|01|06|00|00|00|00|01|1A|0F|00|00|00|00|00|00|80|00|00|AA" to std::vector<uint8_t> ?
- Assistant
To convert a string in the format you mentioned ("55|01|06|00|00|00|00|01|1A|0F|00|00|00|00|00|00|80|00|00|AA") to a
std::vector<uint8_t>, you can follow these steps in C++:- Include the necessary headers:
#include <cstdint> #include <string> #include <sstream> #include <vector>- Create a function that takes the input string and returns the
std::vector<uint8_t>:
std::vector<uint8_t> convertStringToBytes(const std::string& input) { std::vector<uint8_t> bytes; // Split the input string by '|' std::stringstream ss(input); std::string byteString; while (std::getline(ss, byteString, '|')) { // Convert each byte string to uint8_t and append to the vector uint8_t byte = std::stoul(byteString, nullptr, 16); bytes.push_back(byte); } return bytes; }