Created
January 10, 2024 07:44
-
-
Save AndreiCherniaev/fd57f8e5af74ab40e4c0775de4913a40 to your computer and use it in GitHub Desktop.
str2arr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
#include <cstdlib> | |
#include <string> | |
#include <vector> | |
unsigned char parse_hex(char c) | |
{ | |
if ('0' <= c && c <= '9') return c - '0'; | |
if ('A' <= c && c <= 'F') return c - 'A' + 10; | |
if ('a' <= c && c <= 'f') return c - 'a' + 10; | |
std::abort(); | |
} | |
std::vector<unsigned char> parse_string(const std::string & s) | |
{ | |
if (s.size() % 2 != 0) std::abort(); | |
std::vector<unsigned char> result(s.size() / 2); | |
for (std::size_t i = 0; i != s.size() / 2; ++i) | |
result[i] = 16 * parse_hex(s[2 * i]) + parse_hex(s[2 * i + 1]); | |
return result; | |
} | |
int main() | |
{ | |
std::vector<unsigned char> v= parse_string("5502FD302900000006C1AA5502FD302900000006C1AA5502FD302900000006C1AA5502FD302900000006C1AA5502FD302900000006C1AA55"); | |
for(auto &val : v){ | |
printf("0x%02X, ", val); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment