Convert string to uint

I am trying to read a hex value from a text box and then put it in uint, this is the code:

UInt32 x = Convert.ToUInt32(location_txtbox.ToString());

Why I want to pass x to unmanaged code, and the function header requires a DWORD.

Am I getting an "input string" not in the correct format error? I'm trying to enter values ​​like 0x7c or 0x777, but I keep getting errors?

Thank.

+3
source share
4 answers

Use this Convert.ToUInt32 overload where you specify the base of the transform. It should work with or without the "0x" master.

UInt32 x = Convert.ToUInt32(location_txtbox.Text, 16);
+14
source

, location_txtbox TextBox, .Text .ToString().

, 16 hex: . Convert.UInt32 (X, 16)

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

+3

Convert.ToUInt32 UInt32.Parse(String). , NumberStyles, , . , "0x", .

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

A) Convert, try/catch, , , , .

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

C) in a style that seems like you will need NumberStyles.HexSpecifier

D) Use the Text property as described above.

+2
source

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


All Articles