简体   繁体   中英

How to split a hexadecimal string into an array in c++?

I have this string

string in = "Two One Nine Two";

I convert it to hexadecimal using the following function

    std::string string_to_hex(const std::string& input)
{
    static const char* const lut = "0123456789ABCDEF";
    size_t len = input.length();

    std::string output;
    output.reserve(2 * len);
    for (size_t i = 0; i < len; ++i)
    {
        const unsigned char c = input[i];
        output.push_back(lut[c >> 4]);
        output.push_back(lut[c & 15]);
    }
    return output;
}

now, how to split it into an array like this

int plain[16] = {0x54,0x77,0x6F,0x20,0x4F,0x6E,0x65,0x20,0x4E,0x69,0x6E,0x65,0x20,0x54,0x77,0x6F};

This should help:

string in = "Two One Nine Two";
strncpy(&plain[0], in.c_str(), 16);

The string literal is already stored in memory in the format that you want.
I'm showing one method to copy it to an array of character.

If I understand what you are trying to do...

What you need to do is tokenize the input string with the space being the delimiter.

Once you have that it is a simple matter of matching the words to numbers - a simple compare or use if == will do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM