- public static int ToInt32(string value)
- {
- if (value == null)
- {
- return 0;
- }
- return int.Parse(value, CultureInfo.CurrentCulture);
- }
复制代码
在内部其实也是调用了int.Parse
- public static int Parse(string s, IFormatProvider provider)
- {
- return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
- }
复制代码
int.Parse内部有调用了Number.ParseInt32
- [SecuritySafeCritical]
- internal unsafe static int ParseInt32(string s, NumberStyles style, NumberFormatInfo info)
- {
- byte* stackBuffer = stackalloc byte[(UIntPtr)Number.NumberBuffer.NumberBufferBytes / 1];
- Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
- int result = 0;
- Number.StringToNumber(s, style, ref numberBuffer, info, false);
- if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
- {
- if (!Number.HexNumberToInt32(ref numberBuffer, ref result))
- {
- throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
- }
- }
- else
- {
- if (!Number.NumberToInt32(ref numberBuffer, ref result))
- {
- throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
- }
- }
- return result;
- }
复制代码
Number.ParseInt32 最终实现了转换 |