How would I create a C macro to get the integer value of a string? The specific use-case is following on from a question here. I want to change code like this:
enum insn {
sysenter = (uint64_t)'r' << 56 | (uint64_t)'e' << 48 |
(uint64_t)'t' << 40 | (uint64_t)'n' << 32 |
(uint64_t)'e' << 24 | (uint64_t)'s' << 16 |
(uint64_t)'y' << 8 | (uint64_t)'s',
mov = (uint64_t)'v' << 16 | (uint64_t)'o' << 8 |
(uint64_t)'m'
};
To this:
enum insn {
sysenter = INSN_TO_ENUM("sysenter"),
mov = INSN_TO_ENUM("mov")
};
Where INSN_TO_ENUM expands to the same code. The performance would be the same, but the readability would be boosted by a lot.
I'm suspecting that in this form it might not be possible because of a the C preprocessor's inability for string processing, so this would also be an unpreferred but acceptable solution (variable argument macro):
enum insn {
sysenter = INSN_TO_ENUM('s','y','s','e','n','t','e','r'),
mov = INSN_TO_ENUM('m','o','v')
};
enum insn { sysenter, mov };and be happy with sequential numbering. What problem are you trying to solve?