0

Can someone edit this to look better? Thanks!

I'm trying to have a string that is a Hex value but it is set by a third party (Not The Code Itself) and set it back to an int but still be a Hex value. C# Will let you have an int that is equal to a Hex if you have the 0x in front.

Code:

string HexValue = "0x0FC";
int OtherValue = Convert.ToInt(HexValue);

When I try this I get: 'Input string was not in a correct format.'

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • This question already has an answer here: http://stackoverflow.com/a/4280019/2850543 – Millie Smith Mar 21 '17 at 03:05
  • Possible duplicate of [Convert a string containing a hexadecimal value starting with "0x" to an integer or long](http://stackoverflow.com/questions/4279892/convert-a-string-containing-a-hexadecimal-value-starting-with-0x-to-an-integer) – Millie Smith Mar 21 '17 at 03:05

1 Answers1

1

You can convert string into a int hex value by adding this System.Globalization.NumberStyles.HexNumber:

string HexValue = "0x0FC";
int OtherValue = int.Parse(HexValue.Substring(2),System.Globalization.NumberStyles.HexNumber);

Hope it helps!

mindOfAi
  • 4,412
  • 2
  • 16
  • 27