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
  • 16,091
  • 29
  • 100
  • 164

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
  • 138,677
  • 31
  • 291
  • 286
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
  • 24,703
  • 9
  • 76
  • 110
1

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

Tigran
  • 61,654
  • 8
  • 86
  • 123