Jump to content
Sign in to follow this  
jacmac

How do I put a #defined constant into a string?

Recommended Posts

If I have this define:

#define OLIVEDRAB	'#6B8E23'

How do I get '#6B8E23' into a string, for example:

_staticmsg = OLIVEDRAB;
hintC _staticmsg;

does not display '#6B8E23', instead it displays 6B8E23. Wrapping quotes around the constant results in the constant not being replaced with the #define, so I get OLIVEDRAB instead of '#6B8E23'. Are my defines bad for what I'm trying to do or is there a method to get the whole defined constant (including the ' 's) into a string variable?

Share this post


Link to post
Share on other sites

#define OLIVEDRAB	'#6B8E23'
hint format["%1", str(OLIVEDRAB)];

That might work?

the command str will make it into a string and wrap the " around it.

Share this post


Link to post
Share on other sites

What about this:

#define OLIVEDRAB	"'#6B8E23'"

hintC OLIVEDRAB;

Share this post


Link to post
Share on other sites

I like the str(OLIVEDRAB) idea, I had not thought of it and will try it out. #define OLIVEDRAB "'#6B8E23'" Wouldn't work out because there are a number of situations where the constant does not need to go into a string variable. I could have two versions of the define, one with and one without the quotes, but I would like to avoid that if possible.

Share this post


Link to post
Share on other sites

Unfortunately str (OLIVEDRAB) doesn't work either. It produces 6B8E23 missing both ' and #. It also appears that # causes some issues. I tried removing the ' from the #define so that the define looked like this:

#define OLIVEDRAB  #6B8E23

What str (OLIVEDRAB) produces in that case is simply 6. A #define is not handled as expected if is a string with ' and # also causes issues with preprocessing.

As a side note #define OLIVEDRAB "'#6B8E23'" does work, so I added a whole set of new #defines with RGB_colorname "'#xxxxxx'"

Edited by Jacmac

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×