471,325 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

TryParse with HexNumber returning wrong value

Ok, Someone tell me what I'm doing wrong. The statement below is
returning 527.0 in v, instead of the expected 215. Am I missing
something?

double v;

if (Double.TryParse("0d7", NumberStyles.HexNumber,
NumberFormatInfo.InvariantInfo, out v))
{
......
}

Thanks,

Greg
Dec 12 '05 #1
5 5404
Greg,

It's not returning anything for me, actually, it throws an exception.
In .NET 2.0, you can not use the hex modifier on floating point values.

If you are using .NET 2.0, then I would recommend using the TryParse
method on the Int32 structure. If not, then I would call the Parse method,
and catch the exception.

Then I would convert the int to a double through a cast.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Greg Wilkerson" <gr***********@charter.net> wrote in message
news:jk********************************@4ax.com...
Ok, Someone tell me what I'm doing wrong. The statement below is
returning 527.0 in v, instead of the expected 215. Am I missing
something?

double v;

if (Double.TryParse("0d7", NumberStyles.HexNumber,
NumberFormatInfo.InvariantInfo, out v))
{
......
}

Thanks,

Greg

Dec 12 '05 #2
That's interesting. I thought I was on .NET 2.0. I'll have to
upgrade that. Did you get it to work properly using the Int32
structure, or did you try? I'll get my .NET upgraded and try the
Int32.

Thanks.

On Mon, 12 Dec 2005 12:21:27 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
Greg,

It's not returning anything for me, actually, it throws an exception.
In .NET 2.0, you can not use the hex modifier on floating point values.

If you are using .NET 2.0, then I would recommend using the TryParse
method on the Int32 structure. If not, then I would call the Parse method,
and catch the exception.

Then I would convert the int to a double through a cast.

Hope this helps.

Dec 12 '05 #3
Greg,

It worked fine when I ran this using the TryParse method on Int32.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Greg Wilkerson" <gr***********@charter.net> wrote in message
news:2b********************************@4ax.com...
That's interesting. I thought I was on .NET 2.0. I'll have to
upgrade that. Did you get it to work properly using the Int32
structure, or did you try? I'll get my .NET upgraded and try the
Int32.

Thanks.

On Mon, 12 Dec 2005 12:21:27 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
Greg,

It's not returning anything for me, actually, it throws an exception.
In .NET 2.0, you can not use the hex modifier on floating point values.

If you are using .NET 2.0, then I would recommend using the TryParse
method on the Int32 structure. If not, then I would call the Parse
method,
and catch the exception.

Then I would convert the int to a double through a cast.

Hope this helps.

Dec 12 '05 #4
Well, I thought there was a reason I had not upgraded to .NET 2.0; VS
2003 won't use it.

So, I'll just catch a parse exception. Th'at's probably all the
TryParse does, anyway.

On a side note, I did think it was odd to provide a hex conversion for
a double.

Thanks for you help.

Greg

On Mon, 12 Dec 2005 12:41:43 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
Greg,

It worked fine when I ran this using the TryParse method on Int32.

Dec 12 '05 #5
Greg,

Actually, the TryParse does not just catch the exception. The whole
point of the TryParse method is to reduce the overhead involved with having
to catch an exception if the string can not be parsed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Greg Wilkerson" <gr***********@charter.net> wrote in message
news:6v********************************@4ax.com...
Well, I thought there was a reason I had not upgraded to .NET 2.0; VS
2003 won't use it.

So, I'll just catch a parse exception. Th'at's probably all the
TryParse does, anyway.

On a side note, I did think it was odd to provide a hex conversion for
a double.

Thanks for you help.

Greg

On Mon, 12 Dec 2005 12:41:43 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
Greg,

It worked fine when I ran this using the TryParse method on Int32.

Dec 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Joachim | last post: by
5 posts views Thread by MC | last post: by
1 post views Thread by John A Grandy | last post: by
3 posts views Thread by bungle | last post: by
5 posts views Thread by Steffan A. Cline | last post: by
1 post views Thread by dehranph | last post: by
3 posts views Thread by nautonnier | last post: by
2 posts views Thread by steven | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.