C宏:将数字转换为字符串 [英] C macro : turn a number into a string

查看:674
本文介绍了C宏:将数字转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,用于定义符号在5x7点显示屏上的外观。

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.

表的开头部分与ASCII表匹配,后跟一组特殊字符符号,例如箭头或复选标记。要引用这些宏,我有一个宏列表:

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;

问题:如何转为 SYMBOL_RIGHT_ARROW 放入 \x79,或整个字符串放入 Next\x79 在编译时,这样我就可以R / O部分中的字符串?

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编译器。

Freescale HC08 C-compiler.

推荐答案

您可以在C源代码中连接字符串:

You can concatenate strings in C source:

printf("%s\n", "forty" "two"); /* prints "fortytwo" */
/* NOTE:             ^^^ no punctuation */

使用符号来完成这项工作很多,但也许可以忍受。

To do that with your symbols is a lot of work, but maybe you can live with that.

#define SYMBOL_LEFT_ARROW 120
#define SYMBOL_LEFT_ARROW_STR "\x79"
#define SYMBOL_RIGHT_ARROW (SYMBOL_LEFT_ARROW + 1)
#define SYMBOL_RIGHT_ARROW_STR "\x83"
const char * const message = "Next" SYMBOL_RIGHT_ARROW_STR;



更新



如果您可以使符号的值与其在符号表中的位置匹配(120个匹配 \x78),请尝试以下宏

UPDATE

If you can make the value of the symbol match its position in the symbol table (120 match "\x78"), try these macros

#include <stdio.h>

#define ADD_ZERO_X(y) 0x ## y
#define SYMBOL_NUM(x) ADD_ZERO_X(x)

#define STRINGIZE(z) #z
#define ADD_SLASH_X(y) STRINGIZE(\x ## y)
#define SYMBOL_STR(x) ADD_SLASH_X(x)

#define SYMBOL_LEFT_ARROW 78 /* must write in hexadecimal without any prefix */
#define SYMBOL_RIGHT_ARROW 79
#define SYMBOL_UP_ARROW 7a

int main(void) {
  printf("%d\n", SYMBOL_NUM(SYMBOL_LEFT_ARROW));
  printf("%s\n", SYMBOL_STR(SYMBOL_LEFT_ARROW));
  printf("%d\n", SYMBOL_NUM(SYMBOL_RIGHT_ARROW));
  printf("%s\n", SYMBOL_STR(SYMBOL_RIGHT_ARROW));
  printf("%d\n", SYMBOL_NUM(SYMBOL_UP_ARROW));
  printf("%s\n", SYMBOL_STR(SYMBOL_UP_ARROW));
  return 0;
}






编辑(所以不喜欢我的浏览器)

宏扩展后 SYMBOL_NUM(32)转换为整数文字( 0x78 );和 SYMBOL_STR(78)转换为字符串文字( \x78 )。

After macro expansion SYMBOL_NUM(32) is transformed to a integer literal (0x78); and SYMBOL_STR(78) is transformed to a string literal ("\x78").

您可以像直接键入文字一样使用文字。

You can use the literals as if you had typed them in.

const char *test = "Next" SYMBOL_STR(78) " one";
/* same as
   const char *test = "Next\x78 one";
*/

这篇关于C宏:将数字转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆