C#的Convert.ToInt32实现

以下是Convert.ToInt32(String str)的个人实现:

代码
    public class ConvertHelper
    {
        
private const int POSITIVE_SIGN_NUM = 43;
        
private const int PLUS_SIGN_NUM = 45;
        
private const int CHAR_0_NUM = 48;
        
private const int CHAR_9_NUM = 57;

        
/// <summary>
        
/// convert to int32
        
/// </summary>
        
/// <param name="str"></param>
        
/// <returns></returns>
        public static int ToInt32(string str)
        {
            
if (str == null || str.Trim().Length < 1)
            {
                
throw new ArgumentNullException("str");
            }

            str 
= str.Trim();
            
int firstCharNum = str[0];
            
if (firstCharNum > CHAR_9_NUM)
            {
                
throw new FormatException();
            }

            
int number = 0;
            
if (firstCharNum < CHAR_0_NUM)
            {
                
if (str.Length == 1)
                {
                    
throw new FormatException();
                }

                
bool isPositive = true;
                
switch (firstCharNum)
                {
                    
case POSITIVE_SIGN_NUM:
                        
break;
                    
case PLUS_SIGN_NUM:
                        isPositive 
= false;
                        
break;
                    
default:
                        
throw new FormatException();
                }

                
for (int i = 1; i < str.Length; i++)
                {
                    
if (str[i] < CHAR_0_NUM || str[i] > CHAR_9_NUM)
                    {
                        number 
= isPositive ? number : 0 - number;
                        
break;
                    }
                    
else
                    {
                        Check(str, i, 
true);
                        number 
= number * 10 + str[i] - CHAR_0_NUM;
                    }
                }
            }
            
else
            {
                
for (int i = 0; i < str.Length; i++)
                {
                    
if (str[i] < CHAR_0_NUM || str[i] > CHAR_9_NUM)
                    {
                        
break;
                    }
                    
else
                    {
                        Check(str, i, 
true);
                        number 
= number * 10 + str[i] - CHAR_0_NUM;
                    }
                }
            }

            
return number;
        }

        
/// <summary>
        
/// check number if overflow
        
/// </summary>
        
/// <param name="number">string number</param>
        
/// <param name="index"></param>
        
/// <param name="isPositiveData"></param>
        private static void Check(string number, int index, bool isPositiveData)
        {
            
string maxOrMinValue = isPositiveData ? int.MaxValue.ToString() : int.MinValue.ToString();

            
if (index == maxOrMinValue.Length)
            {
                
for (int j = 0; j < index; j++)
                {
                    
if (number[j] < maxOrMinValue[j])
                    {
                        
break;
                    }

                    
if (number[j] > maxOrMinValue[j])
                    {
                        
throw new OverflowException();
                    }
                }
            }
            
else
            {
                
if (index > maxOrMinValue.Length)
                {
                    
throw new OverflowException();
                }
            }
        }
    }

 这里,需要注意的几点是:

1. 如果传参为空,需要判断处理;

2. 首位字符可能是:+/-,0~9;

3. 字符串中可能包含非数字字符;

4. 溢出处理;

5. 效率。

原文地址:https://www.cnblogs.com/Langzi127/p/1690051.html