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,954 | Comments: 44,412

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

  1. What is new in RavenDB 3.5–Intro - 18 hours from now

There are posts all the way to Jul 08, 2015

RECENT SERIES

  1. The RavenDB Comic Strip (3):
    28 May 2015 - Part III – High availability & sleeping soundly
  2. Special Offer (2):
    27 May 2015 - 29% discount for all our products
  3. RavenDB Sharding (3):
    22 May 2015 - Adding a new shard to an existing cluster, splitting the shard
  4. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  5. Interview question (2):
    30 Mar 2015 - fix the index
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats