1

Short Version:

I have a String: 0x4D;0x90;0x69

I want an array

static const uint8_t array[] = {
    0x4D, 0x90, 0x69
} 

How to do?

Longer Version:

I have an String (buffer) with like this: 0x4D​0x90​0x69 between those hex "numbers" are Zero Width Spaces, and I split those up into a vector of strings using

std::vector<std::string> v{ explode(buffer, '\u200B') };

I want for to have an Vector with uint8_t data in it.

I already tried to reinterpret_cast the string, and that actually worked. But I have put it in a forloop an it should push the result into an uint8_t Vector, but in the Vector were only 0x00 in it.

std::vector < std::string > v {
  explode(buffer, '\u200B')
};
std::vector < uint8_t * > ob;
for (auto n: v) {
  uint8_t * p = reinterpret_cast < uint8_t * > ( & n);
  //std::cout << n << " | " << p << std::endl;
  ob.push_back(p);
};

for (auto na : ob) std::cout << na << std::endl;

I only get three 0x00 in the console.

I want to have an static const uint8_t arr[] containing the buffer splited up.

Edit: I have forgot to add the explodefunction here, its basicly just a split. ```cpp

const std::vector<std::string> explode(const std::string& s, const char& c)
{
    std::string buff{ "" };
    std::vector<std::string> v;
    for (auto n : s)
    {
        if (n != c) buff += n; else
            if (n == c && buff != "") { v.push_back(buff); buff = ""; }
    }
    if (buff != "") v.push_back(buff);

    return v;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Florian sp1rit
  • 575
  • 1
  • 7
  • 20
  • Have you tried `std::atoi`? https://en.cppreference.com/w/cpp/string/byte/atoi – Silvano Cerza Jun 12 '19 at 11:01
  • 3
    What is `explode`? I don't recall there is a function called `explode` in the standard library. – L. F. Jun 12 '19 at 11:02
  • @Someprogrammerdude std::string uses char, I think he/she/x wants unsigned char instead – nada Jun 12 '19 at 11:03
  • 2
    Set the size of the destination vector (it should be the same as the source vector). Then use [`std::transform`](https://en.cppreference.com/w/cpp/algorithm/transform) with a [lambda](https://en.cppreference.com/w/cpp/language/lambda) that calls [`std::stoi`](https://en.cppreference.com/w/cpp/string/basic_string/stol) to convert the strings. – Some programmer dude Jun 12 '19 at 11:04
  • 1
    And remember that in most systems `uint8_t` is an alias of `unsigned char` and outputting a `char` (plain, signed or unsigned) will output that as a *character* and not a small integer. You need to [convert](https://en.cppreference.com/w/cpp/language/static_cast) the byte to an `unsigned`. – Some programmer dude Jun 12 '19 at 11:06
  • 1
    Problem this code should solve is badly defined. Also code it self doesn't explain the intent. So I have no idea what the actual problem is. For me it looks like [XY problem](http://xyproblem.info/) - bad solution for some simple problem which is not explained. – Marek R Jun 12 '19 at 11:11
  • If you could show us what the vector `v` actually contains, may be we could understand what you are really trying to do... – Serge Ballesta Jun 12 '19 at 11:29
  • Seems pretty clear that OP wants to interpret each string in a vector as the hex representation of a number, and lexically convert them to integers (`uint8_t`). This is certainly not the way to do it though. (Also the whole zero-width-space thing is completely irrelevant and should be abstracted away.) – Lightness Races in Orbit Jun 12 '19 at 11:31
  • In fact if I didn't also want to point out the UB this would just be Yet Another String To Int question to dupehammer! – Lightness Races in Orbit Jun 12 '19 at 11:32
  • 1
    I believe `std::cout << na << std::endl;` will still try to print the `char` type, even after conversion to `uint8_t`. You need to cast them to an `int` to get a decimal number. And you need to use `std::cout << std::hex << na << std::endl;` for a hexadecimal number. – jww Jun 12 '19 at 11:33
  • @jww Unfortunately it's not a decimal number but a fake view over the contents of a `std::string`! – Lightness Races in Orbit Jun 12 '19 at 11:37

1 Answers1

0
for (auto n : v)

This takes a copy of each thing in v which lives for the duration of the loop body.

uint8_t * p = reinterpret_cast < uint8_t * > ( & n);

This gives you a pointer to the local copied thing after reinterpretation.

ob.push_back(p);

This stores the pointer into a vector.

}

This makes all those pointers dangling.

To fix this UB, you could try for (const auto& n : v) instead.

However, there are other problems. Each of those "things" is a std::string! It is meaningless to cast a std::string* to uint8_t* and not really clear what you meant to achieve with that. Did you mean to cast the result of n.c_str() to uint8_t*? But then that is not really achieving anything either.

I think you need to look into a better way to interpret a sequence of characters as a number and convert it to an integer.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I need it to be an uint8_t because its actually lot more that three numbers. Its actually a binary executable stored in an AES encryped format wich I unencrypt to then execute without having the unencrypted version stored on the Harddrive. – Florian sp1rit Jun 12 '19 at 14:27
  • @ShadowCode That's fine but you're producing your `uint8_t`s in a completely wrong way. You can't just slap a `reinterpret_cast` on some random object and get the job done. It doesn't perform "logical" transformation in the way you're apparently imagining, and it certainly doesn't do so on the constituent bytes of the machinery of a `std::string`. – Lightness Races in Orbit Jun 12 '19 at 14:28
  • @ Lightness Races in Orbit yeah, I'm not good at C++ / C. I now have slightly reworked the way i recieve the numbers. I now have a vector with ints. Now I want to convert one int at a time to hex and it to be an `uint8_t`. (I always just find int to hex string) – Florian sp1rit Jun 12 '19 at 15:55
  • @ShadowCode I linked to that topic. How to parse a string to obtain an `int` is well-covered here. But you do have to parse it. You can't just slap a cast on it! – Lightness Races in Orbit Jun 12 '19 at 16:01