Specify the hex representation when converting enum to string : Enum « Data Type « Visual C++ .NET






Specify the hex representation when converting enum to string

 

#include "stdafx.h"
using namespace System;


[ Flags ]
enum class Color
{
    Red = 1,
    Blue = 2,
    Green = 4   // use powers of 2
};

int main()
{
   Console::WriteLine("Colors: {0}, {1}, {2}", Color::Red, Color::Blue,Color::Green);
   Console::WriteLine("Colors: {0:d}, {1:d}, {2:d}", Color::Red, Color::Blue,Color::Green);

   Color c = Color::Red | Color::Blue;

   String^ s1 = c.ToString("X"); 
   Console::WriteLine( s1 );


   String^ s2 = Enum::Format( Color::typeid, c , "G");

   Console::WriteLine(s2 );
}

   
  








Related examples in the same category

1.Create an Enumeration
2.Enum Type
3.enum type specified
4.enum format
5.Use the bitwise OR operator to combine flags
6.Use the Format method of the Enum class
7.enum class type