Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,026 | Comments: 44,842

filter by tags archive

What the hell?! Hex number parsing

time to read 1 min | 139 words

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

    Console.WriteLine(long.Parse("0x9f",NumberStyles.AllowHexSpecifier|NumberStyles.HexNumber, CultureInfo.InvariantCulture));
catch (Exception 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.


Uncle Festis

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

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

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

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

Ayende Rahien


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

Uncle Festis & Demis,

I don't want to drop that.

Demis Bellot

Are you reading these docs?


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

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

The s parameter contains a number of the form:


Or, if style includes AllowHexSpecifier:




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


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.


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.


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


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

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.

Comment preview

Comments have been closed on this topic.


No future posts left, oh my!


  1. Technical observations from my wife (3):
    13 Nov 2015 - Production issues
  2. Production postmortem (13):
    13 Nov 2015 - The case of the “it is slow on that machine (only)”
  3. Speaking (5):
    09 Nov 2015 - Community talk in Kiev, Ukraine–What does it take to be a good developer
  4. Find the bug (5):
    11 Sep 2015 - The concurrent memory buster
  5. Buffer allocation strategies (3):
    09 Sep 2015 - Bad usage patterns
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats