« Samsung i730 Evaluation | Main | Amazing post on startup & shutdown »

How to convert a hex string to an integer

The MSDN page that tries to answer this question is an absolutely horrible example of how a compact and simple piece of information can be made almost impossible to understand: How to use the Int32.Parse method to convert the string representation of a hexadecimal integer to a decimal integer by using Visual C# .NET

Here is what it should have said:

To convert a hexadecimal string to an integer:

string s = "a0"; // 160 in hexadecimal

int i = int.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier);

 

NOTE: If the string includes the standard "0x" prefix, a System.FormatException will occur.

 

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)