We help IT Professionals succeed at work.

translate #defines

minnirok
minnirok asked
on
Medium Priority
360 Views
Last Modified: 2013-11-20
Hi,

Is there anyway to convert a #define from a string into it's numeric value:

    "SS_BLACKFRAME"

for instance should be intrepreted as whatever ms defined as its real numeric value:

    "SS_BLACKFRAME"  =  SS_BLACKFRAME  =  0x07L

I'm just trying to write a small interpreter, and I don't want to have a function to translate them like:

    int TranslateDefs(CString str)
    {
        if (str == "SS_BLACKFRAME") {
            return 0x07L;
        }
        // etc...
    }

Thank you
Comment
Watch Question

mahesh1402IT Professional
Top Expert 2005

Commented:
whats exactly you are looking for ?
#define is Preprocessor directive that means compile time your values will be changes with whatever you define it as.

MAHESH

Author

Commented:
yeah ms must have this someplace, probably in windows.h or something:

    #define SS_BLACKFRAME 0x07L

Now I'm reading a text file that has this on a line:

    "SS_BLACKFRAME"

I want to be able to store the defined value of SS_BLACKFRAME in a DWORD:

    DWORD dwStyle;

    dwStyle = SomeFunkyThingToTurnTheTextRepresentationIntoTheNumericVal("SS_BLACKFRAME");

Thanks
CERTIFIED EXPERT
Top Expert 2012
Commented:
Use a map:

#include <map>
usinag namespace std;

map<char*,DWORD> mapNameToId;

mapNameToId["SS_BLACKFRAME"] = SS_BLACKFRAME;

or

#define ADD_ENTRY(x) mapNameToId[#x] = x;
//...
ADD_ENTRY(SS_BLACKFRAME);

DWORD SomeFunkyThingToTurnTheTextRepresentationIntoTheNumericVal(char* x) {

    return mapNameToId[x];
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Ok just wanted to see if there was something to already translate. This should work ok though.

Thanks.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.