Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to parse string to integer value

I really don't have the idea on why I am getting 0 value on this:

enter image description here

But this code works well:

int val = Convert.ToInt32("1546");    

Here is the appsetting:

<add key="PesoPayMerchantId" value="​1546"/>

Any idea?

Edit1

I want to get the integer value of "1546", but it fails to work.
Here is the code for getting appsetting:

    public static string GetConfigurationString(string appSettingValue)
    {
        return ConfigurationManager.AppSettings[appSettingValue];
    }  

I have tried your suggestions, and this is the result:

enter image description here
enter image description here
enter image description here

The string value is correct ("1546"), but it can't be parse to integer. What is happening here?

Edit 2

I am very sure that the value of:

<add key="PesoPayMerchantId" value="​1546"/>  

is really a combination of numbers "1546"
But when I try to re-write the string value using Immediate Window it can now be parsed. But still I can't figure out the very reason of this Bug?

enter image description here

Edit 3

Finally, it works now, thanks to Johnny

What I did is, I re-write the whole, <add key="PesoPayMerchantId" value="1546"/> and it can now be parsed. Thanks for all your help. :D

like image 642
fiberOptics Avatar asked Mar 14 '14 05:03

fiberOptics


People also ask

How do I parse a string with integer values?

By default, strings that represent integer values are parsed by using the NumberStyles.Integer value, which permits only numeric digits, leading and trailing white space, and a leading sign. Strings that represent floating-point values are parsed using a combination of the NumberStyles.Float...

What happens if a numeric parsing method is passed to string?

If a numeric parsing method is passed a string that contains any other digits, the method throws a FormatException. The following example uses the Int32.Parse method to parse strings that consist of digits in different writing systems.

How to convert string to integer in Java?

The method generally used to convert String to Integer in Java is parseInt (). There are two variants of this method: 1. parseInt (String s): This function parses the string argument as a signed decimal integer.

Is it possible to parse hexadecimal values in a string?

The currency symbol is permitted. The currency symbol is defined by the NumberFormatInfo.CurrencySymbol property. The string to be parsed is interpreted as a hexadecimal number. It can include the hexadecimal digits 0-9, A-F, and a-f. This flag can be used only to parse integer values.


2 Answers

The answer would be, re-write the config.
As I remember, I just copied and paste "1546" from a pdf file.
So lesson learned, don't be too lazy on typing values.

Additional Information:
I also remember that, I did copy and paste on gmail (google Chrome) and I found out that the text I copied contains hidden characters at the beginning.

like image 66
fiberOptics Avatar answered Nov 10 '22 00:11

fiberOptics


I can only think that you are experiencing some kind of weird globalization / Culture specific problem.

Given that you know the exact format of the number, you might try the Int32.TryParse Method (String, NumberStyles, IFormatProvider, Int32) overload, e.g.:

int.TryParse(val, NumberStyles.Any, CultureInfo.InvariantCulture, out id);
like image 25
StuartLC Avatar answered Nov 10 '22 00:11

StuartLC