C 宏:将数字转为字符串
我有一个表,定义 5x7 点显示屏上的符号外观。类似于:
extern UINT8 symbols[][5] = {
{0x0,0x0,0x0,0x0,0x0},
{0x0,0x0,0x5F,0x0,0x0},
{0x0,0x7,0x0,0x7,0x0},
{0x14,0x7F,0x14,0x7F,0x14}, // etc.
表的前导部分与 ASCII 表匹配,后跟一组特殊符号,例如箭头或复选标记。为了引用这些宏,我有一个宏列表:
#define SYMBOL_LEFT_ARROW 120 // 120 is the entry in the table
#define SYMBOL_RIGHT_ARROW (SYMBOL_LEFT_ARROW+1)
#define SYMBOL_UP_ARROW (SYMBOL_RIGHT_ARROW+1)
现在我需要说一些类似的话(不会编译):
const char * const message = "Next" + SYMBOL_RIGHT_ARROW;
问题:如何将SYMBOL_RIGHT_ARROW
转换为“\x79” ,或将整个字符串放入 "Next\x79"
AT COMPILE TIME 这样我就可以将字符串放在 R/O 部分中?
飞思卡尔 HC08 C 编译器。
I have a table that defines symbols appearance on a 5x7 dot display. Something like:
extern UINT8 symbols[][5] = {
{0x0,0x0,0x0,0x0,0x0},
{0x0,0x0,0x5F,0x0,0x0},
{0x0,0x7,0x0,0x7,0x0},
{0x14,0x7F,0x14,0x7F,0x14}, // etc.
The leading part of the table matches ASCII table, followed by a set of special symbols, e.g. an arrow, or a check-mark. To reference those I have a list of macros:
#define SYMBOL_LEFT_ARROW 120 // 120 is the entry in the table
#define SYMBOL_RIGHT_ARROW (SYMBOL_LEFT_ARROW+1)
#define SYMBOL_UP_ARROW (SYMBOL_RIGHT_ARROW+1)
Now I need to say something like (won't compile):
const char * const message = "Next" + SYMBOL_RIGHT_ARROW;
Question: How do I turn SYMBOL_RIGHT_ARROW
into "\x79", or whole string into "Next\x79"
AT COMPILE TIME so I can have the string in R/O section?
Freescale HC08 C-compiler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 C 源代码中连接字符串:
使用符号来连接字符串需要大量工作,但也许您可以忍受。
更新
如果您可以使符号的值与其在符号表中的位置相匹配(120 匹配“\x78”),请尝试使用这些宏
编辑(SO 不喜欢我的浏览器)< /em>
宏扩展后
SYMBOL_NUM(32)
转换为整数文字 (0x78
);并且SYMBOL_STR(78)
转换为字符串文字 ("\x78"
)。您可以像输入文字一样使用这些文字。
You can concatenate strings in C source:
To do that with your symbols is a lot of work, but maybe you can live with that.
UPDATE
If you can make the value of the symbol match its position in the symbol table (120 match "\x78"), try these macros
Edit (SO doesn't like my browser)
After macro expansion
SYMBOL_NUM(32)
is transformed to a integer literal (0x78
); andSYMBOL_STR(78)
is transformed to a string literal ("\x78"
).You can use the literals as if you had typed them in.
我想出了这个小程序:
宏中的间接寻址是必要的,以便您的字符在“#”将其转换为字符串之前得到扩展。请注意,当您以这种方式使用 '\x77' 值时,它会变成有效的 int...
I came up with this little program:
the indirection in the macro is necessary so that your character gets expanded before the "#" turns it into a string. notice that the '\x77' value turns into a valid int when you use it that way...
这是我能想到的最好的,虽然不完美,但可以放入 ROM 中:
This is best I could come up with, not perfect, but can be put in ROM: