4

Possible Duplicate:
How to convert numbers between hexadecimal and decimal in C#?

I need to be able to take a hexadecimal string and convert it into actual hexadecimal value in .NET. How do I do this?

For instance, in Delphi, you can take string of "FF" and add the dollar sign as follow to it.

tmpstr := '$'+ 'FF';

Then, convert tmpstr string variable into an integer to get the actual hexidecimal. The result would be 255.

Community
  • 1
  • 1
ThN
  • 3,235
  • 3
  • 57
  • 115
  • 5
    Check this out: http://stackoverflow.com/questions/74148/how-to-convert-numbers-between-hexadecimal-and-decimal-in-c – dontangg May 23 '12 at 15:58

4 Answers4

10

Assuming you are trying to convert your string to an int:

var i = Int32.Parse("FF", System.Globalization.NumberStyles.HexNumber)

Your example 1847504890 does not fit on an int, however. Use a longer type instead.

var i = Int64.Parse("1847504890", System.Globalization.NumberStyles.HexNumber)
user703016
  • 37,307
  • 8
  • 87
  • 112
3

Very simple:

int value = Convert.ToInt32("DEADBEEF", 16);

Speed Of Light
  • 106
  • 2
  • 9
3

You can do it by following

string tmpstr = "FF";
int num = Int32.Parse(tmpstr, System.Globalization.NumberStyles.HexNumber);

You can also see the link Converting string to hex

Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38
1
int hexval = Convert.ToInt32("FF", 16);
Mithrandir
  • 24,869
  • 6
  • 50
  • 66