zoukankan      html  css  js  c++  java
  • 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. 效率。

  • 相关阅读:
    在springboot程序中自定义注解和反序列化实现
    文章相似度算法调研
    HTTP协议详解
    prototype.js 让你更深入的了解javascript的面向对象特性(转)
    ajax框架汇总
    prototype源码分析(转)
    c#中静态成员和实例成员(转)
    .NET中IDisposable接口的基本使用 (转)
    sql server 数据库优化(转)
    ADO.NET事物
  • 原文地址:https://www.cnblogs.com/Langzi127/p/1690051.html
Copyright © 2011-2022 走看看