I have a string which typically is in the format "0xFF". I'll trim it since there is a chance of whitespace. How do i convert that into hex and convert "34" to decimal. I know about .Parse but does this support hex characters when the string is 0x123?
+2
A:
int i = int.Parse( "FF", System.Globalization.NumberStyles.HexNumber );
MessageBox.Show( i.ToString() ); // displays 255
However, you will need to trim the leading "0x
".
Ed Swangren
2009-07-11 03:23:19
+3
A:
You'll have to strip the "0x" part, but this snippet works:
using System;
using System.Globalization;
public class StrToInt {
public static void Main(string[] args) {
string val = "FF";
int num = Int32.Parse(val, NumberStyles.AllowHexSpecifier);
Console.WriteLine(num);
}
}
ars
2009-07-11 03:24:36