Convert to string an enum item

Alex Parrill via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 23 09:43:52 PST 2015


On Wednesday, 23 December 2015 at 13:11:28 UTC, tcak wrote:
> [code]
> import std.stdio;
> import std.conv;
>
> enum Values: ubyte{ One = 1, Two = 2 }
>
> void main(){
> 	writeln( std.conv.to!string( Values.One ) );
> }
> [/code]
>
> Output is "One".
>
> casting works, but to be able to cast correctly, I need to tell 
> compiler that it is "ubyte".
>
> Isn't there any NON-HACKISH solution to print out the value of 
> enum item? And do not decrease the performance as well please. 
> It runs on web server.
>

So you want the enum's integer value? Try casting it to an 
integer.

writeln(cast(ubyte) Values.One);


More information about the Digitalmars-d-learn mailing list