[ACCEPTED]-converting string to uint-visual-studio-2008

Accepted answer
Score: 14

Use this overload of Convert.ToUInt32 where you specify the base of 2 the conversion. It should work with or 1 without the leading "0x".

UInt32 x = Convert.ToUInt32(location_txtbox.Text, 16);
Score: 3

Assuming location_txtbox is a TextBox, you 3 may want to use .Text instead of .ToString().

And 2 I think you need to pass 16 as the second 1 parameter to parse hex: e.g. Convert.UInt32( X, 16)

See: http://msdn.microsoft.com/en-us/library/swz6z5ks.aspx

Score: 2

Convert.ToUInt32 just calls UInt32.Parse(String). If you call the overload that takes 4 a NumberStyles parameter, you can specify your value 3 is hexadecimal. Trouble is, you'll have 2 to cut out the leading "0x", as 1 it isn't allowed.

var hexNumber = "0x777".Substring(2);
uint.Parse(hexNumber, System.Globalization.NumberStyles.HexNumber)
Score: 2

A) Never use Convert without wrapping it 8 in a try/catch, or unless you are VERY sure 7 that what you are parsing will be parsed 6 successfully.

B) Use uint.TryParse. In particular, use 5 the one that allows you to specify the number 4 style. The reference for it is at http://msdn.microsoft.com/en-us/library/kadka85s.aspx

C) in 3 the style it appears that you will need 2 NumberStyles.HexSpecifier

D) Use the Text 1 property, as stated above.

More Related questions