0

I have a function that allows access to system variables based on a lookup code. The lookup code has an enum type.

typedef enum SystemVariable_e { INT_SYSTEM_VAR_1 = 0x10, INT_SYSTEM_VAR_2 = 0x20 //etc.. } SystemVariable_e; uint16_t ReturnValue (SystemVariable_e lookup_code) { if(lookup_code == INT_SYSTEM_VAR_1) { return system_variable_one; } //etc.. } 

Sometimes I need to store these system variables in a (const char *) so that they can be parsed by a function that reads strings.

To make these strings somewhat legible, I have been using #define for the lookup codes.

#define STR_SYSTEM_VAR_1 "\x10" const char * StringToParse = "\x01" STR_SYSTEM_VAR_1 "\x80"; 

In its current state, my code uses two different lookup codes (INT_SYSTEM_VAR_1 and STR_SYSTEM_VAR_1) which use the same underlying number (0x10) to access the same system variable (system_variable_one).

I want to either merge the #define into the enum, or add the enum to the (const char *), so that I don't have two names for one lookup code.

Here are some of the things I have tried:

//this fails but describes my goal #define STR_SYSTEM_VAR_1 "\x10" typedef enum SystemVariable_e { SYSTEM_VAR_1 = STR_SYSTEM_VAR_1[0], } SystemVariable_e; 
//this fails but describes my goal typedef enum SystemVariable_e { INT_SYSTEM_VAR_1 = 0x10 } SystemVariable_e; const char * StringToParse = "\x01" {INT_SYSTEM_VAR_1,'\0'} "\x80"; 

1 Answer 1

0

The simplest would be to just:

typedef enum SystemVariable_e { INT_SYSTEM_VAR_1 = 0x10 } SystemVariable_e; const char * StringToParse = (char[]){'\x01',INT_SYSTEM_VAR_1,'\x80',0}; 

But I would anyway do a lookup table:

// tostring.h // for ((i=0;i<256;++i)); do printf "#define TOSTRING_0x%02x \"\\\\x%02x\"\n" "$i" "$i"; done #define TOSTRING_0x00 "\x00" #define TOSTRING_0x01 "\x01" #define TOSTRING_0x02 "\x02" #define TOSTRING_0x03 "\x03" #define TOSTRING_0x04 "\x04" #define TOSTRING_0x05 "\x05" #define TOSTRING_0x06 "\x06" #define TOSTRING_0x07 "\x07" #define TOSTRING_0x08 "\x08" #define TOSTRING_0x09 "\x09" #define TOSTRING_0x0a "\x0a" #define TOSTRING_0x0b "\x0b" #define TOSTRING_0x0c "\x0c" #define TOSTRING_0x0d "\x0d" #define TOSTRING_0x0e "\x0e" #define TOSTRING_0x0f "\x0f" #define TOSTRING_0x10 "\x10" #define TOSTRING_0x11 "\x11" #define TOSTRING_0x12 "\x12" #define TOSTRING_0x13 "\x13" #define TOSTRING_0x14 "\x14" #define TOSTRING_0x15 "\x15" #define TOSTRING_0x16 "\x16" #define TOSTRING_0x17 "\x17" #define TOSTRING_0x18 "\x18" #define TOSTRING_0x19 "\x19" #define TOSTRING_0x1a "\x1a" #define TOSTRING_0x1b "\x1b" #define TOSTRING_0x1c "\x1c" #define TOSTRING_0x1d "\x1d" #define TOSTRING_0x1e "\x1e" #define TOSTRING_0x1f "\x1f" #define TOSTRING_0x20 "\x20" #define TOSTRING_0x21 "\x21" #define TOSTRING_0x22 "\x22" #define TOSTRING_0x23 "\x23" #define TOSTRING_0x24 "\x24" #define TOSTRING_0x25 "\x25" #define TOSTRING_0x26 "\x26" #define TOSTRING_0x27 "\x27" #define TOSTRING_0x28 "\x28" #define TOSTRING_0x29 "\x29" #define TOSTRING_0x2a "\x2a" #define TOSTRING_0x2b "\x2b" #define TOSTRING_0x2c "\x2c" #define TOSTRING_0x2d "\x2d" #define TOSTRING_0x2e "\x2e" #define TOSTRING_0x2f "\x2f" #define TOSTRING_0x30 "\x30" #define TOSTRING_0x31 "\x31" #define TOSTRING_0x32 "\x32" #define TOSTRING_0x33 "\x33" #define TOSTRING_0x34 "\x34" #define TOSTRING_0x35 "\x35" #define TOSTRING_0x36 "\x36" #define TOSTRING_0x37 "\x37" #define TOSTRING_0x38 "\x38" #define TOSTRING_0x39 "\x39" #define TOSTRING_0x3a "\x3a" #define TOSTRING_0x3b "\x3b" #define TOSTRING_0x3c "\x3c" #define TOSTRING_0x3d "\x3d" #define TOSTRING_0x3e "\x3e" #define TOSTRING_0x3f "\x3f" #define TOSTRING_0x40 "\x40" #define TOSTRING_0x41 "\x41" #define TOSTRING_0x42 "\x42" #define TOSTRING_0x43 "\x43" #define TOSTRING_0x44 "\x44" #define TOSTRING_0x45 "\x45" #define TOSTRING_0x46 "\x46" #define TOSTRING_0x47 "\x47" #define TOSTRING_0x48 "\x48" #define TOSTRING_0x49 "\x49" #define TOSTRING_0x4a "\x4a" #define TOSTRING_0x4b "\x4b" #define TOSTRING_0x4c "\x4c" #define TOSTRING_0x4d "\x4d" #define TOSTRING_0x4e "\x4e" #define TOSTRING_0x4f "\x4f" #define TOSTRING_0x50 "\x50" #define TOSTRING_0x51 "\x51" #define TOSTRING_0x52 "\x52" #define TOSTRING_0x53 "\x53" #define TOSTRING_0x54 "\x54" #define TOSTRING_0x55 "\x55" #define TOSTRING_0x56 "\x56" #define TOSTRING_0x57 "\x57" #define TOSTRING_0x58 "\x58" #define TOSTRING_0x59 "\x59" #define TOSTRING_0x5a "\x5a" #define TOSTRING_0x5b "\x5b" #define TOSTRING_0x5c "\x5c" #define TOSTRING_0x5d "\x5d" #define TOSTRING_0x5e "\x5e" #define TOSTRING_0x5f "\x5f" #define TOSTRING_0x60 "\x60" #define TOSTRING_0x61 "\x61" #define TOSTRING_0x62 "\x62" #define TOSTRING_0x63 "\x63" #define TOSTRING_0x64 "\x64" #define TOSTRING_0x65 "\x65" #define TOSTRING_0x66 "\x66" #define TOSTRING_0x67 "\x67" #define TOSTRING_0x68 "\x68" #define TOSTRING_0x69 "\x69" #define TOSTRING_0x6a "\x6a" #define TOSTRING_0x6b "\x6b" #define TOSTRING_0x6c "\x6c" #define TOSTRING_0x6d "\x6d" #define TOSTRING_0x6e "\x6e" #define TOSTRING_0x6f "\x6f" #define TOSTRING_0x70 "\x70" #define TOSTRING_0x71 "\x71" #define TOSTRING_0x72 "\x72" #define TOSTRING_0x73 "\x73" #define TOSTRING_0x74 "\x74" #define TOSTRING_0x75 "\x75" #define TOSTRING_0x76 "\x76" #define TOSTRING_0x77 "\x77" #define TOSTRING_0x78 "\x78" #define TOSTRING_0x79 "\x79" #define TOSTRING_0x7a "\x7a" #define TOSTRING_0x7b "\x7b" #define TOSTRING_0x7c "\x7c" #define TOSTRING_0x7d "\x7d" #define TOSTRING_0x7e "\x7e" #define TOSTRING_0x7f "\x7f" #define TOSTRING_0x80 "\x80" #define TOSTRING_0x81 "\x81" #define TOSTRING_0x82 "\x82" #define TOSTRING_0x83 "\x83" #define TOSTRING_0x84 "\x84" #define TOSTRING_0x85 "\x85" #define TOSTRING_0x86 "\x86" #define TOSTRING_0x87 "\x87" #define TOSTRING_0x88 "\x88" #define TOSTRING_0x89 "\x89" #define TOSTRING_0x8a "\x8a" #define TOSTRING_0x8b "\x8b" #define TOSTRING_0x8c "\x8c" #define TOSTRING_0x8d "\x8d" #define TOSTRING_0x8e "\x8e" #define TOSTRING_0x8f "\x8f" #define TOSTRING_0x90 "\x90" #define TOSTRING_0x91 "\x91" #define TOSTRING_0x92 "\x92" #define TOSTRING_0x93 "\x93" #define TOSTRING_0x94 "\x94" #define TOSTRING_0x95 "\x95" #define TOSTRING_0x96 "\x96" #define TOSTRING_0x97 "\x97" #define TOSTRING_0x98 "\x98" #define TOSTRING_0x99 "\x99" #define TOSTRING_0x9a "\x9a" #define TOSTRING_0x9b "\x9b" #define TOSTRING_0x9c "\x9c" #define TOSTRING_0x9d "\x9d" #define TOSTRING_0x9e "\x9e" #define TOSTRING_0x9f "\x9f" #define TOSTRING_0xa0 "\xa0" #define TOSTRING_0xa1 "\xa1" #define TOSTRING_0xa2 "\xa2" #define TOSTRING_0xa3 "\xa3" #define TOSTRING_0xa4 "\xa4" #define TOSTRING_0xa5 "\xa5" #define TOSTRING_0xa6 "\xa6" #define TOSTRING_0xa7 "\xa7" #define TOSTRING_0xa8 "\xa8" #define TOSTRING_0xa9 "\xa9" #define TOSTRING_0xaa "\xaa" #define TOSTRING_0xab "\xab" #define TOSTRING_0xac "\xac" #define TOSTRING_0xad "\xad" #define TOSTRING_0xae "\xae" #define TOSTRING_0xaf "\xaf" #define TOSTRING_0xb0 "\xb0" #define TOSTRING_0xb1 "\xb1" #define TOSTRING_0xb2 "\xb2" #define TOSTRING_0xb3 "\xb3" #define TOSTRING_0xb4 "\xb4" #define TOSTRING_0xb5 "\xb5" #define TOSTRING_0xb6 "\xb6" #define TOSTRING_0xb7 "\xb7" #define TOSTRING_0xb8 "\xb8" #define TOSTRING_0xb9 "\xb9" #define TOSTRING_0xba "\xba" #define TOSTRING_0xbb "\xbb" #define TOSTRING_0xbc "\xbc" #define TOSTRING_0xbd "\xbd" #define TOSTRING_0xbe "\xbe" #define TOSTRING_0xbf "\xbf" #define TOSTRING_0xc0 "\xc0" #define TOSTRING_0xc1 "\xc1" #define TOSTRING_0xc2 "\xc2" #define TOSTRING_0xc3 "\xc3" #define TOSTRING_0xc4 "\xc4" #define TOSTRING_0xc5 "\xc5" #define TOSTRING_0xc6 "\xc6" #define TOSTRING_0xc7 "\xc7" #define TOSTRING_0xc8 "\xc8" #define TOSTRING_0xc9 "\xc9" #define TOSTRING_0xca "\xca" #define TOSTRING_0xcb "\xcb" #define TOSTRING_0xcc "\xcc" #define TOSTRING_0xcd "\xcd" #define TOSTRING_0xce "\xce" #define TOSTRING_0xcf "\xcf" #define TOSTRING_0xd0 "\xd0" #define TOSTRING_0xd1 "\xd1" #define TOSTRING_0xd2 "\xd2" #define TOSTRING_0xd3 "\xd3" #define TOSTRING_0xd4 "\xd4" #define TOSTRING_0xd5 "\xd5" #define TOSTRING_0xd6 "\xd6" #define TOSTRING_0xd7 "\xd7" #define TOSTRING_0xd8 "\xd8" #define TOSTRING_0xd9 "\xd9" #define TOSTRING_0xda "\xda" #define TOSTRING_0xdb "\xdb" #define TOSTRING_0xdc "\xdc" #define TOSTRING_0xdd "\xdd" #define TOSTRING_0xde "\xde" #define TOSTRING_0xdf "\xdf" #define TOSTRING_0xe0 "\xe0" #define TOSTRING_0xe1 "\xe1" #define TOSTRING_0xe2 "\xe2" #define TOSTRING_0xe3 "\xe3" #define TOSTRING_0xe4 "\xe4" #define TOSTRING_0xe5 "\xe5" #define TOSTRING_0xe6 "\xe6" #define TOSTRING_0xe7 "\xe7" #define TOSTRING_0xe8 "\xe8" #define TOSTRING_0xe9 "\xe9" #define TOSTRING_0xea "\xea" #define TOSTRING_0xeb "\xeb" #define TOSTRING_0xec "\xec" #define TOSTRING_0xed "\xed" #define TOSTRING_0xee "\xee" #define TOSTRING_0xef "\xef" #define TOSTRING_0xf0 "\xf0" #define TOSTRING_0xf1 "\xf1" #define TOSTRING_0xf2 "\xf2" #define TOSTRING_0xf3 "\xf3" #define TOSTRING_0xf4 "\xf4" #define TOSTRING_0xf5 "\xf5" #define TOSTRING_0xf6 "\xf6" #define TOSTRING_0xf7 "\xf7" #define TOSTRING_0xf8 "\xf8" #define TOSTRING_0xf9 "\xf9" #define TOSTRING_0xfa "\xfa" #define TOSTRING_0xfb "\xfb" #define TOSTRING_0xfc "\xfc" #define TOSTRING_0xfd "\xfd" #define TOSTRING_0xfe "\xfe" #define TOSTRING_0xff "\xff" #define TOSTRING_IN(x) TOSTRING_##x #define TOSTRING(x) TOSTRING_IN(x) 

and then just:

#define INT_SYSTEM_VAR_1 0x10 const char * StringToParse = "\x01" TOSTRING(INT_SYSTEM_VAR_1) "\x80"; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.