LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to convert strings or characters to enum type during runtime?

I know that it does not make sense, but it would be handy and maybe there is some vicked trick out there?

 

The issue is that I have a string and I would like to use the characters as indices to access a 2D matrix.

 

The indices do not follow the alphabetical order (it would be easy to serialize the Ascii codes by reducing their value by A) but

in enum space A=3, B=8, C=1.... etc

Currently I use switch {} and case 'A':  value =3... but this is not economical, I have to have 23 case statements!

Tried some type casting, it does not seem to work

 

typedef enum  {A=3,B=5,C=1,D=22,E}LetterIndex;
char *string[]={"AAABBBCCCDDDEEE"};

LetterIndex Lind[21];

 

than assing: Lind[0]=(LetterIndex) string[0]; and get the ascii value of 'A' instead of 3 (as in teh enum, of course becouse of diffrent storage of A and the 'A'

 

I would wellcome a good idea here...

 

Layosh

0 Kudos
Message 1 of 4
(3,770 Views)

Well if your string definition was replaced with something more along the lines of:

 

    char string[]={A,A,A,B,B,B,C,C,C,D,D,D,E,E,E};

 

you might be a bit closer to achieving your goal, but I'm not certain about what you want your final code to do.

 

JR

0 Kudos
Message 2 of 4
(3,764 Views)

to answer your original question, it is not possible to get the enum value from a string in C. (however, the reverse is possible using the stringizing preprocessor operator, but it will not help in your case.)

 

back to your problem, i can see no problem getting the enum value from a character by subtracting 'A'. here is how i would do it:

 

// this is the original enum defining the 

typedef enum
{
    A=3,
    B=5,
    C=12,
    D=22,
    E=48,

    ...

} index;

 

// this array is a lookup table: it shall be indexed by letters starting with 'A'

// and contains the enum value associated with each letter

index lut['Z'-'A'+1] = {A,B,C,D,E, ...};

 

// if this is the input string and the ouput array

char  string[21]= "AAABBBCCDDDDDEABDECD";
index lind[21];  // adjust the size of this array, or allocate it dynamically to suit your needs

 

// now conversion takes the form of 

unsigned int i = 0;

while ( string[i] != '\0' )

{

    if ( (string[i] < 'A') && (string[i] > 'Z') )  // you shall always verify user input !

        exit( -1 );                                // take appropriate actions in case of input error

 

    lind[i] = lut[ string[i]-'A' ];

    i++;

}

 

 

there can be a great number of variations on this code: using string replace functions of the standard library, using characters pointers to factorout the index of the string, etc...

 

anyway, i fear that you got your problem wrong. what you are writing looks a lot like a parser: a user provides an input that you read and convert to something useful for your program. if the input string is given by a user, this solution works (while there may be a more extensible solution). remember, that user input shall always be validated, and error checking code might take a lot more than the single line in the example above because a parser has to be fool-proof and fail gracefully in case of error. however, if the input string is coded in your own program, then this approach is definitely bad: inefficient, error prone, the solution does not represent the problem space. if you tell us more about your program, we would be able to tell you more about a solution.

 

 

Message 3 of 4
(3,760 Views)

Thank you! It is the solution! I probalby had the 'writer's block' creeping on me, you knocked me out of it 🙂

layosh

0 Kudos
Message 4 of 4
(3,738 Views)