2

I want to get hex from a string like this:

Color c = new Color();
c.A = Int32.Parse("0x7F"); 

What's the right method for this ?

patrick
  • 15,066
  • 27
  • 96
  • 157

3 Answers3

4

If the hex string isn't known till runtime, then something like this:

c.A = Convert.ToInt32("0x7F", 16);

Or as a literal if the value is known at compile time:

    c.A = 0x7F;
vcsjones
  • 133,702
  • 30
  • 291
  • 279
2

With the Parse method, check the overload which allows NumberStyles. Reference http://msdn.microsoft.com/en-us/library/c09yxbyt.aspx

int bla = Int32.Parse("beef", NumberStyles.HexNumber);
Matthew
  • 23,095
  • 7
  • 70
  • 102
1

Convert.ToInt32("hexvaluestring", 16); Should be enough.

Tigran
  • 60,656
  • 8
  • 83
  • 120