HexNumber Issues

  • Thread starter Thread starter bungle
  • Start date Start date
B

bungle

Hi,

Would anyone be able to explain why I get a "Input string was not in a
correct format" on the following bit of code in C#??

public static int GENERIC_READ =
int.Parse("0xH80000000",System.Globalization.NumberStyles.HexNumber,
null);

Thanks

Dave Court
 
Hi,
Would anyone be able to explain why I get a "Input string was not in a
correct format" on the following bit of code in C#??

public static int GENERIC_READ =
int.Parse("0xH80000000",System.Globalization.NumberStyles.HexNumber,
null);

Thanks

Dave Court

You don't want the "0xH" prefix there, you already specify that it's a
hexnumber.

And why not use:
public static uint GENERIC_READ = 0x80000000;

(note: the compiler requires a Uint here)


Hans Kesting
 
Hi Dave,

Because the documentation says, "Strings parsed with this style are not
permitted to be prefixed with 0x".
[System.Globalization.NumberStyles.AllowHexSpecifier]

HexNumber incorporates the AllowHexSpecifier flag in its value.

The code works if you remove the "0xH" prefix.

Hans mentioned in his response that the number is greater than what fits in a
signed Int32. The Parse method won't throw an exception but you might not get
the value you expect. Try uint and uint.Parse instead if that causes a
problem for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top