使用C预处理获取字符串的整数值 [英] Using C Preprocessing to get integer value of a string
问题描述
如何创建 C 宏以获取字符串的整数值?具体问题在以下此处中进行.我想更改这样的代码:
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'
};
对此:
enum insn {
sysenter = INSN_TO_ENUM("sysenter"),
mov = INSN_TO_ENUM("mov")
};
INSN_TO_ENUM
扩展为相同代码的位置.性能将是相同的,但是可读性将大大提高.
Where INSN_TO_ENUM
expands to the same code. The performance would be the same, but the readability would be boosted by a lot.
我怀疑由于C预处理器无法进行字符串处理,因此以这种形式可能无法实现,因此这也是一种不受欢迎但可以接受的解决方案(可变参数宏):
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')
};
推荐答案
这是一个编译时的纯C解决方案,您指出可以接受.您可能需要将其扩展为更长的助记符.我将继续考虑所需的那个(即INSN_TO_ENUM("sysenter")
).有趣的问题:)
Here's a compile-time, pure C solution, which you indicated as acceptable. You may need to extend it for longer mnemonics. I'll keep on thinking about the desired one (i.e. INSN_TO_ENUM("sysenter")
). Interesting question :)
#include <stdio.h>
#define head(h, t...) h
#define tail(h, t...) t
#define A(n, c...) (((long long) (head(c))) << (n)) | B(n + 8, tail(c))
#define B(n, c...) (((long long) (head(c))) << (n)) | C(n + 8, tail(c))
#define C(n, c...) (((long long) (head(c))) << (n)) | D(n + 8, tail(c))
#define D(n, c...) (((long long) (head(c))) << (n)) | E(n + 8, tail(c))
#define E(n, c...) (((long long) (head(c))) << (n)) | F(n + 8, tail(c))
#define F(n, c...) (((long long) (head(c))) << (n)) | G(n + 8, tail(c))
#define G(n, c...) (((long long) (head(c))) << (n)) | H(n + 8, tail(c))
#define H(n, c...) (((long long) (head(c))) << (n)) /* extend here */
#define INSN_TO_ENUM(c...) A(0, c, 0, 0, 0, 0, 0, 0, 0)
enum insn {
sysenter = INSN_TO_ENUM('s','y','s','e','n','t','e','r'),
mov = INSN_TO_ENUM('m','o','v')
};
int main()
{
printf("sysenter = %llx\nmov = %x\n", sysenter, mov);
return 0;
}
这篇关于使用C预处理获取字符串的整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!