是否有 C# 的 atoi 实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10657836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:42:09  来源:igfitidea点击:

Is there an atoi implementation for C#

c#atoi

提问by Aaron Fischer

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..

我有一个数字保存为字符串的文件。我希望将此数据作为 char[] 加载,然后将此数组转换为整数值。我有几个值要加载,并试图避免创建数千个临时字符串的开销。

回答by FishBasketGordo

There's a couple of ways:

有几种方法:

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

虽然,这些方法处理string对象,而不是char[]. 在 C# 中,大多数情况下,char[]由于内置string类型[MSDN],您不会像在 C 中那样处理。

回答by Ed S.

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.

我希望将此数据作为 char[] 加载,然后将此数组转换为整数值。

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

是否有特殊原因使用 achar[]而不是仅从文件中读取字符串?无论如何,您都需要将 astring转换为 an int(使用标准转换方法),因此这似乎是一个浪费的步骤。

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 intwill not create needless temporaries.

您应该只需要为从文件中读取的每个字符串创建一个字符串。我不知道这里涉及哪些其他处理,但是从文件中读取字符串然后转换为int不会创建不必要的临时文件。

If you are performing a large/unknown number of string concatenations then you should use a StringBuilderto 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 farlonger to read the data from disk then it will take to create these strings.

如果您正在执行大量/未知数量的字符串连接,那么您应该使用 a StringBuilder来构建您的输入,因为它在幕后充当可变字符串(在创建时也将其初始化为足够大的大小)。即便如此,它(应该)采取再从磁盘读取数据,然后它会创建这些字符串。

回答by Aaron Fischer

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.htmlwith the string replaced with char[]

来自http://www.csharptricks.com/blog/2006/09/string-to-integer-in-c-net.html,字符串替换为 char[]

回答by Alexandre R

here is also a possible implementation of atoi()

这里也是 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;
}

回答by Tony C

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