Ayende @ Rahien

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

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 5,968 | Comments: 44,488

filter by tags archive

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.


Comments

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

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

Uncle Festis & Demis,

I don't want to drop that.

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

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

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

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

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

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.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
  2. Production postmortem (4):
    23 Jul 2015 - The case of the native memory leak
  3. API Design (7):
    20 Jul 2015 - We’ll let the users sort it out
  4. What is new in RavenDB 3.5 (3):
    15 Jul 2015 - Exploring data in the dark
  5. The RavenDB Comic Strip (3):
    28 May 2015 - Part III – High availability & sleeping soundly
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats