C 宏:将数字转为字符串

发布于 2024-08-08 13:11:40 字数 764 浏览 3 评论 0原文

我有一个表,定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半暖夏伤 2024-08-15 13:11:40

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

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

使用符号来连接字符串需要大量工作,但也许您可以忍受。

#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”),请尝试使用这些宏

#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;
}

编辑(SO 不喜欢我的浏览器)< /em>

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

您可以像输入文字一样使用这些文字。

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

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;

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;
}

Edit (SO doesn't like my browser)

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";
*/
面犯桃花 2024-08-15 13:11:40

我想出了这个小程序:

#include <stdio.h>

#define TEST_CHR '\x77'
#define VAL(x) #x
#define STRINGIFY(x) VAL(x)
int main()
{
   int x = TEST_CHR;
   char *yyy = "%d " STRINGIFY(TEST_CHR) "\n";

   printf(yyy,x);

   return 0;

}

宏中的间接寻址是必要的,以便您的字符在“#”将其转换为字符串之前得到扩展。请注意,当您以这种方式使用 '\x77' 值时,它会变成有效的 int...

I came up with this little program:

#include <stdio.h>

#define TEST_CHR '\x77'
#define VAL(x) #x
#define STRINGIFY(x) VAL(x)
int main()
{
   int x = TEST_CHR;
   char *yyy = "%d " STRINGIFY(TEST_CHR) "\n";

   printf(yyy,x);

   return 0;

}

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...

薄凉少年不暖心 2024-08-15 13:11:40

这是我能想到的最好的,虽然不完美,但可以放入 ROM 中:

const char message[] = {'N','e','x','t',SYMBOL_RIGHT_ARROW,EOS};

This is best I could come up with, not perfect, but can be put in ROM:

const char message[] = {'N','e','x','t',SYMBOL_RIGHT_ARROW,EOS};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文