473,691 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.TryPars e("0d7", NumberStyles.He xNumber,
NumberFormatInf o.InvariantInfo , out v))
{
......
}

Thanks,

Greg
Dec 12 '05 #1
5 5561
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.co m

"Greg Wilkerson" <gr***********@ charter.net> wrote in message
news:jk******** *************** *********@4ax.c om...
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.TryPars e("0d7", NumberStyles.He xNumber,
NumberFormatInf o.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.c om> 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.co m

"Greg Wilkerson" <gr***********@ charter.net> wrote in message
news:2b******** *************** *********@4ax.c om...
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.c om> 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.c om> 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.co m

"Greg Wilkerson" <gr***********@ charter.net> wrote in message
news:6v******** *************** *********@4ax.c om...
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.c om> wrote:
Greg,

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

Dec 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3325
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value for my first element but my subsequent printf's will return garbage. Can someone let me know what I am doing wrong. Thanks in advance. -jlewis
1
2655
by: Joachim | last post by:
When I run double.TryParse("12.01234567890123456789", out value) I only get 12.0123456789012 as result. How can I get double.TryParse to translate more than 13 decimals (if possible)?
5
2135
by: MC | last post by:
Let's say I want to validate something with TryParse but I don't need to convert it. In C#, it seems I must provide that out parameter regardless. In VB, I can pass Nothing. I'd like to be able to do the same in C# instead of having to declare a "dummy" variable for the out parameter. In other words, can I do something like this: if( int.TryParse( str, null ) ) { // Do something. }
1
5704
by: John A Grandy | last post by:
Primitives like Int32 provide a Parse() method , and TryParse() method -- which is very useful. Enum provides a Parse() method, but not a TryParse() method. Other than wrapping the Enum.Parse() in an exception handler , can someone recommend a technique similar to TryParse() ? Thanks.
3
1528
by: bungle | last post by:
Hi, Would anyone be able to explain why I get a "Input string was not in a correct format" on the following bit of code in C#?? public static int GENERIC_READ = int.Parse("0xH80000000",System.Globalization.NumberStyles.HexNumber, null); Thanks
5
8748
by: Steffan A. Cline | last post by:
When using this as indicated and trying to set it to accept multiple formats, it bombs out with the error: Overload resolution failed because no accessible 'TryParse' accepts this number of arguments The code looks like this: ok = Decimal.TryParse(tbIll.Text, _ NumberStyles.AllowCurrencySymbol,
1
6859
by: dehranph | last post by:
I have this code that always return zero whenever i passed 1,000,00 in a da-DK culture or en-US culture. It works with amount less that 1000 like 99,99. but always returns 0 when greater than or equal to 1000. Decimal separator is ",". I suspect its because the decimal and thousand separator are the same. Thread.CurrentThread.CurrentCulture = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name);...
3
2765
by: nautonnier | last post by:
Hello All, I'm validating a textbox to make sure it contains only a whole number so I'm using Int32.TryParse. It works fine when I test it on a local winform app. However, when I transfer the code to my project that's using ClickOnce, it throws an error. Here's the code that's identical in both projects: int parseResult; if (Int32.TryParse(txtNumberTest.Text, out parseResult))
2
35779
by: steven | last post by:
Hi Anybody know why there's no TryParse method for a string type? I can do the following when trying to convert an object to int: object oTest = 1; int iValue; int.TryParse(oTest, iValue); But there's no equivalent for string:
0
8535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9079
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8952
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8793
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8796
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6457
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5817
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4553
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2234
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.