-->

Is there an atoi implementation for C#

2019-09-01 06:51发布

问题:

I have a file with numbers saved as strings. I am looking to load this data as a char[] then convert this array to an integer value. I have several values to load and am trying to avoid the overhead of creating thousands of temp strings..

回答1:

Yup

int.Parse(string)

If you absolutely must convert the strings to character arrays (not sure why), you can use:

int.Parse(new string(myCharArray));

On a further note...

I am looking to load this data as a char[] then convert this array to an integer value.

Is there a particular reason to use a char[] instead of just reading strings from the file? You will need a string to convert to an int anyway (using the standard conversion methods), so it seems like a wasted step.

I have several values to load and am trying to avoid the overhead of creating thousands of temp strings..

You should only need to create one string for each string you read from the file. I don't know what other processing is involved here, but reading a string from the file and then converting to an int will not create needless temporaries.

If you are performing a large/unknown number of string concatenations then you should use a StringBuilder to build your input as it acts as a mutable string under the covers (initialize it to a large enough size when creating it as well). Even still, it will (should) take far longer to read the data from disk then it will take to create these strings.



回答2:

basically

 public static int StringToInt( char[] ch )
        {
            int length = ch.Length;
            int i = 0;
            int lastNumber = 0;
            int returnNumber = 0;
            bool numberNegative = false;
            int startPoint = 0;

            if ( ch[ 0 ] == '-' )
            {
                numberNegative = true;
                startPoint = 1;
            }

            for ( i = startPoint; i < length; i++ )
            {
                if ( ch[ i ] == ' ' )
                {
                    continue;
                }
                else
                {
                    if ( ( ch[ i ] >= '0' ) && ch[ i ] <= '9' )
                    {
                        returnNumber = ch[ i ] - '0';
                        if ( i > 0 )
                            lastNumber = lastNumber * 10;
                        lastNumber = lastNumber + returnNumber;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if ( numberNegative )
                lastNumber = -1 * lastNumber;

            return lastNumber;
        }

Which is from http://www.csharptricks.com/blog/2006/09/string-to-integer-in-c-net.html with the string replaced with char[]



回答3:

There's a couple of ways:

  • Convert.ToInt32 [MSDN]
  • int.Parse [MSDN]

Although, these methods deal with string objects, not char[]. In C#, most of the time, you don't deal with char[] like you do in C because of the built-in string type [MSDN].



回答4:

here is also a possible implementation of atoi()

int atoi(const char *s)
{
    int n=0, neg=0;
    while (isspace(*s)) s++;
    switch (*s) {
        case '-': neg=1;
        case '+': s++;
    }
    /* Compute n as a negative number to avoid overflow on INT_MIN */
    while (isdigit(*s))
        n = 10*n - (*s++ - '0');
    return neg ? n : -n;
}


回答5:

char c = '1';
int i = Convert.ToInt32(c.ToString(), 10); /*base 10 conversion*/


标签: c# atoi