How to convert a hexadecimal "string" to an actual hexadecimal value in .NET?

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

I need to have a hexadecimal string and convert it to the actual hexadecimal value in .NET. How to do it?

For example, in Delphi, you can take the string "FF" and add a dollar sign following it.

tmpstr := '$'+ 'FF'; 

Then convert the string variable tmpstr to an integer to get the actual hexadecimal number. The result will be 255 .

+4
source share
4 answers

Assuming you are trying to convert string to int :

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

However, your example 1847504890 not suitable for int . Use a longer type instead.

 var i = Int64.Parse("1847504890", System.Globalization.NumberStyles.HexNumber) 
+9
source

Very simple:

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

+3
source

You can do this by doing

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

You can also see the Convert string to hexadecimal link.

+3
source
 int hexval = Convert.ToInt32("FF", 16); 
+1
source

Source: https://habr.com/ru/post/1413976/


All Articles