Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cassert>
- static char char2digit(char c) {
- if (c >= '0' && c <= '9')
- return (c - '0');
- if (c >= 'A' && c <= 'F')
- return (c - 'A' + 10);
- if (c >= 'a' && c <= 'f')
- return (c - 'a' + 10);
- assert(0);
- return 0;
- }
- std::vector<char> set(const std::string &hexstring) {
- std::vector<char> bytes;
- size_t idx = 0;
- bool neg = false;
- if (hexstring[idx] == '-') {
- neg = true;
- ++idx;
- }
- if (hexstring[idx] == '0' && hexstring[idx + 1] == 'x') {
- idx += 2;
- }
- size_t size = hexstring.size();
- assert((size - idx) > 0);
- if ((size - idx) % 2 != 0) {
- char c = char2digit(hexstring[idx++]);
- bytes.push_back(c);
- }
- for (; idx < size; ) {
- char c = char2digit(hexstring[idx++]) << 4;
- c += char2digit(hexstring[idx++]);
- bytes.push_back(c);
- }
- return bytes;
- }
- int main() {
- auto v = set("0xaba");
- for (const auto b : v) {
- std::cout << static_cast<int>(static_cast<unsigned char>(b)) << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement