4

How do I convert the following string:

6F

for example, to a normal int? It's hexdecimal value.

Thanks.

Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
  • See http://stackoverflow.com/questions/1214980/convert-a-single-hex-character-to-its-byte-value-in-c/1215076#1215076 – almog.ori Aug 02 '09 at 01:43

3 Answers3

11
string s = "6F";
int i = Int32.Parse(s, NumberStyles.AllowHexSpecifier);
Console.WriteLine(i); // prints "111" to the console

For details on NumberStyles, see MSDN.

jason
  • 236,483
  • 35
  • 423
  • 525
  • 2
    It would actually be better to use int.Parse, rather than int32. – womp Aug 02 '09 at 00:18
  • 5
    Just a style recommendation. If you're declaring your variable as "int", then you should use "int.Parse()". If you're using Int32, then use Int32.Parse(). You should really follow a standard usage, whichever one you pick. – womp Aug 02 '09 at 00:23
  • 2
    @womp, I actually prefer the style Jason used, for some reason it feels more natural it's like saying declare a primitive int and call the method Parse on Int32 class. – Pop Catalin Aug 02 '09 at 01:56
5
 int num = Int32.Parse(strValue, System.Globalization.NumberStyles.HexNumber);
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
Travis
  • 2,170
  • 1
  • 19
  • 21
2

Convert.ToInt32(stringValue,16);

Where last param is base of 16

almog.ori
  • 7,839
  • 1
  • 35
  • 49