0

I'm having some code issue when trying to convert a string to a uint8_t*. Please note I'm a newbie in C++, I'm trying my best to understand it.

My method receives a string:

8D7152D03D05942E403EB59DC2923B3F,E5DE428B05C0CE081E20E870712D7BE4,12796

Which then is split by the delimiter ','. Then, I call a function to convert the strings into uint8_t*.

Somehow, my variable key_ gets the right value but afterwards, the value changes.

struct Key{
    uint8_t *key_;
    uint8_t *iv_;
    uint32_t creation_timestamp;

    ...

    void from_String(string text){

        printf("Text to convert = %s\n", text.c_str());
        stringstream ss(text);
        string item;
        vector<string> splittedStrings;

        while(getline(ss,item,',')){
            splittedStrings.push_back(item);
        }

        if(splittedStrings.size() == 3){
            convert(splittedStrings[0],0);
            printf("Key: %s\n", key_);
            convert(splittedStrings[1],1);
            convert(splittedStrings[2],2);
        }else{
            printf("Error ! Trying to reconstruct message with invalid data!\n");
        }
    }

    ...

    void convert(string toConvert, int i){

        char* endptr = NULL;

        if (i == 0){
            key_ = (uint8_t*) reinterpret_cast<const uint8_t*>(&toConvert[0]);
        }else if (i == 1){
            printf("Key2: %s\n", key_);
            iv_ = (uint8_t*) reinterpret_cast<const uint8_t*>(&toConvert[0]);
        }else if (i == 2){
            creation_timestamp = strtol(toConvert.c_str(), &endptr, 10);
            handleErrors(endptr, toConvert, creation_timestamp);
        }else {
            printf("Error. Operator is not correct!\n");
        }
    }
};

See how the printfs show the value of key_. The output of the program is:

Text to convert = 8D7152D03D05942E403EB59DC2923B3F,E5DE428B05C0CE081E20E870712D7BE4,40653

Key: 8D7152D03D05942E403EB59DC2923B3F

Key2: E5DE428B05C0CE081E20E870712D7BE4

Is this because of any unexpected behavior of reinterpret_cast? I already tried some other ways of converting a string to a uint8_t* but the result was the same. I've tried using a switch instead of the if else conditions, but the result was the same.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 7
    You pass `toConvert` by value. Anything that points to it's data after the function call is invalid since `toConvert` dies when the function ends. – NathanOliver Oct 18 '19 at 14:44
  • 1
    Also: `iv_ = (uint8_t*) reinterpret_cast(&toConvert[0]);` -- What are you trying to do here? This does no conversion. – PaulMcKenzie Oct 18 '19 at 14:49
  • Thank you a lot NathanOliver. I will try that. – Filipe Amador Oct 18 '19 at 15:00
  • I followed the solution to this question https://stackoverflow.com/questions/7664529/converting-a-string-to-uint8-t-array-in-c Paul – Filipe Amador Oct 18 '19 at 15:01
  • @FilipeAmador Yes, but none of those are conversions. You're not changing or converting anything by issuing a `reinterpret_cast`. All you're doing is asking the compiler to pretend that the type is what you say it is. – PaulMcKenzie Oct 18 '19 at 15:12
  • What is it that you're trying to do, exactly? – Lightness Races in Orbit Oct 18 '19 at 15:23
  • I would change the `uint8_t*` members into `std::vector` and have `convert()` copy the source bytes into them as needed. – Remy Lebeau Oct 18 '19 at 15:50
  • Hi Lightness Races in Orbit. Overall I'm trying to send a struct as a string over a socket. In this particular method I'm trying to recreate the structure in the receiving side. This is, convert the string into each field of the struct. So I thought I needed to cast the string into a uint8_t* to do this. – Filipe Amador Oct 19 '19 at 16:52
  • Hi Paul. I see what you're saying. So I'm doing the wrong thing for sure. As I said in the comment above, I wanted to reconstruct the struct by parsing a string with each field separated by commas. – Filipe Amador Oct 19 '19 at 16:55

0 Answers0