I do have a list of string status messages. Each status message has an individual integer status code assinged to it, like this:
{0, "INITIAL STATE"},
{12, "ERROR INVERTER COMMUNICATION"},
{42, "INITIAL CHARGE"},
{158, "MAINTENANCE CHARGE"},
...
In fact the list has about 200 entries, with 256 being the maximum status code. Now I want to reference the respective string for a statuscode (int) I´m reading from the device. I have been looking at using a struct like this:
typedef struct {
int Statuscode;
String StatusString;
} StatusCodes;
and a definition like this:
StatusCodes MyStatuscodes[] = {
{0, "INITIAL STATE"},
{12, "ERROR INVERTER COMMUNICATION"},
{42, "INITIAL CHARGE"},
{158, "MAINTENANCE CHARGE"},
};
My questions:
- When I get a statuscode of e.g. 12, how do I get to the respective string?
- Is there a better way than using a struct?
- Should I be using a "char *" instead of "String"?
- Ideally I would like to move the list of messages references to a .h file, would that have an impact?
I think there must be an easy solution for this, but being new to C++ I´m struggling with finding a good solution, besides maybe converting to JSON and parsing this.
Consider an array of
const char *
.Consider in some error.cpp file
and in some error.h file