Ayende @ Rahien

It's a girl

What the hell?! Hex number parsing

I have the strong feeling that I am missing something here, this fails:

try
{
    Console.WriteLine(long.Parse("0x9f",NumberStyles.AllowHexSpecifier|NumberStyles.HexNumber, CultureInfo.InvariantCulture));
}
catch (Exception e)
{
    Console.WriteLine(e);
}

Unlike the previous challenges I posted, I truly have no idea why this doesn’t work. I moved to use Convert.ToInt64, that does work. But everything I know (including the docs) tell me that this should work.

Tags:

Posted By: Ayende Rahien

Published at

Originally posted at

Comments

Uncle Festis
05/07/2010 12:10 AM by
Uncle Festis

It no likey the "0x" at the beginning?

Jakob Andersen
05/07/2010 12:11 AM by
Jakob Andersen

From the documentation of NumberStyle.AllowHexSpecifier

'Strings parsed using this style are not permitted to be prefixed with "0x".'

btw. NumberStyles.HexNumber includes AllowHexSpecifier already.

Demis Bellot
05/07/2010 12:11 AM by
Demis Bellot

Oren, dropping the '0x' prefix should work, e.g:

long.Parse("9f", NumberStyles.HexNumber)

Ayende Rahien
05/07/2010 12:22 AM by
Ayende Rahien

Jakob,

Thanks, I missed the not.

But What is up with AllowHexSpecifier, the only hex specifier that I know of is 0x

And removing that from the bit pattern doesn't help: NumberStyles.HexNumber & ~NumberStyles.AllowHexSpecifier

Ayende Rahien
05/07/2010 12:23 AM by
Ayende Rahien

Uncle Festis & Demis,

I don't want to drop that.

Demis Bellot
05/07/2010 12:34 AM by
Demis Bellot

Are you reading these docs?

msdn.microsoft.com/.../...obalization.numberstyles(VS.80).aspx

Although its contra-intuitive (wouldn't be the first time) it says:

"AllowHexSpecifier - Indicates that the numeric string represents a hexadecimal value. Valid hexadecimal values include the numeric digits 0-9 and the hexadecimal digits A-F and a-f. Hexadecimal values can be left-padded with zeros. Strings parsed using this style are not permitted to be prefixed with "0x". "

Normally in these situations I support the right behaviour by wrapping it in a 'int FromHex(this string)' extension method and move on until a better solution crosses my path.

Thomas Krause
05/07/2010 12:35 AM by
Thomas Krause

I think AllowHexSpecifier is horribly named. But the documentation is quite clear:

The s parameter contains a number of the form:

[ws][sign]digits[ws]

Or, if style includes AllowHexSpecifier:

[ws]hexdigits[ws]

...

hexdigits

A sequence of hexadecimal digits from 0 through f, or 0 through F.

AllowHexSpecifier tells Parse to treat the number as hexadecimal. HexNumber does the same, but also allows Whitespace before/after.

Jakob Andersen
05/07/2010 12:43 AM by
Jakob Andersen

Ayende,

As i remember: it isn't AllowHexSpecieir that makes the 0x prefix fail, i have never found a way to have long.Parse accept the prefix so i have used:

Convert.ToInt64("0x9f", 16);

Which is the same i asume you ended up with. But i agree with you, the naming of AllowHexSpecifier is REALLY confusing.

Dmitry
05/07/2010 12:58 AM by
Dmitry

You do not need both, NumberStyles.AllowHexSpecifier and NumberStyles.HexNumber, values. NumberStyles.HexNumber equals to AllowLeadingWhite | AllowTrailingWhite | AllowHexSpecifier. And I agree with everyone about the bad naming of the hex specifier.

dave-ilsw
05/07/2010 01:26 AM by
dave-ilsw

The .NET 4 page for the three-argument Int64.Parse method includes this additional tidbit following the Non-composite NumberStyles values table:

"If the NumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value."

Source: msdn.microsoft.com/.../8b520kdx%28v=VS.100%29.aspx

Anon
05/07/2010 04:52 PM by
Anon

I guess the "0x" is a text-editor "atrefact"? In reality, the hex number is 9F, not 0x9F.

Bevan Arps
05/07/2010 11:58 PM by
Bevan Arps

"0x" is a hex specifier with ancestry back into the deep mists of "C" history. It's related to "0" for base 8: 27 == 033 == 0x1B

I'm not sure where it came from, but Delphi's Object Pascal language used $ to prefix int constants.

Also, the W3C uses "#" in the CSS specification.

Comments have been closed on this topic.