0

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};
  • I don't think you understand the assignment. For example, the character `w` is not a hexadecimal value. Maybe you need to convert "Two" to the digit 2 before converting to hex. – Thomas Matthews Dec 11 '15 at 23:06
  • If you iterate over the result from `input.c_str()`, you will notice that it is an array like your `plain`. – Thomas Matthews Dec 11 '15 at 23:07

2 Answers2

0

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.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

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.

Community
  • 1
  • 1
graham.reeds
  • 16,230
  • 17
  • 74
  • 137