By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,949 Members | 1,648 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,949 IT Pros & Developers. It's quick & easy.

enum to string

P: n/a
Is there any easy way to extract the string from enum variable??

e.g. I have

enum TheEnum {ONE, TWO, THREE};

I would like to generate the array of string containing

{"ONE", "TWO", "THREE"}

Thanks in advance !!
Surapong

Sep 8 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <11**********************@o80g2000hse.googlegroups .com>,
surapong <bo*****@gmail.comwrote:
>Is there any easy way to extract the string from enum variable??

e.g. I have

enum TheEnum {ONE, TWO, THREE};

I would like to generate the array of string containing

{"ONE", "TWO", "THREE"}
Check out http://www.comeaucomputing.com/techtalk/#enumtostring
--
Greg Comeau / 4.3.9 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Sep 8 '07 #2

P: n/a
On Sep 7, 11:03 pm, com...@panix.com (Greg Comeau) wrote:
In article <1189219005.343422.194...@o80g2000hse.googlegroups .com>,

surapong <bohe...@gmail.comwrote:
Is there any easy way to extract the string from enum variable??
e.g. I have
enum TheEnum {ONE, TWO, THREE};
I would like to generate the array of string containing
{"ONE", "TWO", "THREE"}

Check outhttp://www.comeaucomputing.com/techtalk/#enumtostring
Along the same lines, you can in general include a file multiple times
to get different views of the same data. Here's how I do it for the
enum problem. File t.h is a "database" of enum names and enum values:

t.h:

E(ONE,1)
E(TWO,2)
E(FOUR,4)
#undef E

t.C:

#include <map>
#include <string>
#include <iostream>

// Make the enum
#define E(x,y) x = y,
enum MyEnum {
#include "t.h"
};

typedef std::map< MyEnum, std::stringEnumMap;

// Make the initializer for the map of enums to strings
#define E(x,y) EnumMap::value_type(x,#x),
EnumMap::value_type init[] = {
#include "t.h"
};

// Initialize the map
EnumMap em( init, &init[sizeof init/sizeof init[0]]);

// Demonstrate
int main(void)
{
std::cout << em[ONE] << "\n";
std::cout << em[TWO] << "\n";
std::cout << em[FOUR] << "\n";
return 0;
}

Sep 8 '07 #3

P: n/a
Thank very much for both replies.. Really appreciated !!!

Surapong L.

Sep 10 '07 #4

P: n/a
On 8 Wrz, 04:36, surapong <bohe...@gmail.comwrote:
Is there any easy way to extract the string from enum variable??

e.g. I have

enum TheEnum {ONE, TWO, THREE};

I would like to generate the array of string containing

{"ONE", "TWO", "THREE"}
If you need something like that:
const char * TheEnumToString(TheEnum enumerator)
{
switch(enumerator)
{
case ONE:
return "ONE";
case TWO:
return "TWO";
case THREE:
return "THREE";
default:
return "UNKNOWN_ENUM_VALUE";;
}
};

Try this http://rssoftware.home.pl/index.php?id=20,0,0,1,0,0


Oct 5 '07 #5

P: n/a
On Sep 7, 10:36 pm, surapong <bohe...@gmail.comwrote:
Is there any easy way to extract the string from enum variable??

e.g. I have

enum TheEnum {ONE, TWO, THREE};

I would like to generate the array of string containing

{"ONE", "TWO", "THREE"}

Thanks in advance !!
Surapong
Here is yet another way -- this one uses iostreams and locales
enum Numbers{ One,Two,Three};
struct Numbershelper:std::locale::facet{
static std::locale::id id;
Numbershelper(int refs=0): std::locale::facet(refs){
std::vector<std::string scratch;
scratch.push_back("One");
scratch.push_back("Two");
scratch.push_back("Three");
labels.insert(label_map::value_type("C",scratch));
scratch.clear();
scratch.push_back("Ein");
scratch.push_back("Zwei");
scratch.push_back("Drei");
labels.insert(label_map::value_type("German",scrat ch));
scratch.clear();
scratch.push_back("Un");
scratch.push_back("Deux");
scratch.push_back("Tres");
labels.insert(label_map::value_type("French",scrat ch));
}
std::string get(std::string lang,Numbers idx)const{
label_map::const_iterator it=labels.find(lang);
if(it==labels.end())
it=labels.find("C");
return it->second.at(idx);
}
typedef std::map<std::string,std::vector<std::string label_map;
label_map labels;
};
std::locale::id Numbershelper::id;

std::ostream&operator<<(std::ostream&os, Numbers n){
std::locale loc=os.getloc();
if(std::has_facet<Numbershelper>(loc)){
Numbershelper const &h=std::use_facet<Numbershelper>(loc);
os<<h.get(loc.name(),n);
}else
os<<n;
return os;
}

int main(int ,char**)
{
std::ostringstream str;
std::locale loc(str.getloc(),new Numbershelper);
str.imbue(loc);
str<<One;
std::string label=str.str();//your string
return 0;
}
This works, except that there are no portable locale names, like I
used here. So "German" and "French" would have to be replaced with
your systems names. The "C" locale is the default for all systems.

And this is the crux of why there really isn't a standard "enum to
string" facility. Anything standardized by ISO (Like C++) has to be
internationalized, and as you can see, this can be quite a chore.

Lance
Oct 6 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.