Macro C: Turn a Number into a String

I have a table that defines the appearance of characters 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 corresponds to the ASCII table, followed by a set of special characters, for example. arrow or reference mark. For reference to them, 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 (will not compile):

 const char * const message = "Next" + SYMBOL_RIGHT_ARROW; 

Question: How do I turn SYMBOL_RIGHT_ARROW into "\ x79" or an entire string into "Next\x79" COMPUTER TIME so that I can have a string in R / O?

C compiler Freescale HC08.

+6
c c-preprocessor
source share
3 answers

You can concatenate strings in source C:

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

There is a lot of work to do with your characters, but maybe you can live with it.

 #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 character value match its position in the character table (matching 120 "\ 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 don't like my browser)

After macro decomposition, SYMBOL_NUM(32) converted to an integer literal ( 0x78 ); and SYMBOL_STR(78) converted to a string literal ( "\x78" ).

You can use literals as if you entered them.

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

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

Macro indirection is necessary for your character to expand before "#" turns it into a string. note that the value "\ x77" turns into a valid int when you use it that way ...

+3
source share

This is the best I could think of, but not perfect, but can be placed in ROM:

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

All Articles