Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

TryParse with HexNumber returning wrong value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Wilkerson - 12 Dec 2005 17:12 GMT
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
Nicholas Paldino [.NET/C# MVP] - 12 Dec 2005 17:21 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.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
[quoted text clipped - 11 lines]
>
> Greg
Greg Wilkerson - 12 Dec 2005 17:39 GMT
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.

>Greg,
>
[quoted text clipped - 8 lines]
>
>    Hope this helps.
Nicholas Paldino [.NET/C# MVP] - 12 Dec 2005 17:41 GMT
Greg,

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

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.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
[quoted text clipped - 16 lines]
>>
>>    Hope this helps.
Greg Wilkerson - 12 Dec 2005 19:50 GMT
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

>Greg,
>
>    It worked fine when I ran this using the TryParse method on Int32.
Nicholas Paldino [.NET/C# MVP] - 12 Dec 2005 21:00 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Well, I thought there was a reason I had not upgraded to .NET 2.0;  VS
> 2003 won't use it.
[quoted text clipped - 12 lines]
>>
>>    It worked fine when I ran this using the TryParse method on Int32.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.