Justin G Justin G - 23 days ago 4x
C# Question

Memory Address System.FormatException

Lets say I have the following:

int x = 0;
string newX = "0x00A3B43C";

How can I set x to the newX string?
I've tried converting it to an int32 but it throws System.FormatException, so that won't work.

Edit: This isn't really a duplicate, I shouldn't have to convert a hex to an int to define it as an int, right?


You can parse the string fairly easily:

int x;
string newX = "0x00A3B43C";

if (int.TryParse(newX.Substring(2), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out x))
    Console.WriteLine("x = {0}", x); // 10728508


You do not need to convert hex notation to an int to assign it to an int, the compiler takes care of it for you. For example:

int y = 0x00A3B43C;
Console.WriteLine("y = {0}", y); // 10728508

In both cases, the console will output the value 10728508.