Int To String And String To Int
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace IntToStringAndStringToInt 8 { 9 class Program 10 { 11 /// <summary> 12 /// int 转 string ,string 转 int 13 /// </summary> 14 /// <param name="args"></param> 15 static void Main(string[] args) 16 { 17 int number = 98765432; 18 intToString(number); 19 string s = "556677889"; 20 stringToInt(s); 21 Console.ReadKey(); 22 } 23 public static void intToString(int num) 24 { 25 string s = string.Empty; 26 int a = 1; 27 List<int> slist = new List<int>(); 28 while (true) 29 { 30 int mi = 1; 31 for (int i = 0; i < a; i++) 32 { 33 mi *= 10; 34 } 35 if (num * 10 / mi != 0) 36 { 37 if (a == 1) 38 { 39 slist.Add(num % mi); 40 } 41 else 42 { 43 slist.Add((num % mi - num % (mi/10)) * 10 / mi); 44 } 45 a++; 46 } 47 else 48 { 49 break; 50 } 51 } 52 for (int j = slist.Count - 1; j >= 0; j--) 53 { 54 s += slist[j]; 55 } 56 57 Console.WriteLine(s); 58 } 59 60 public static void stringToInt(string s) 61 { 62 int sum = 0; 63 for (int i = 0; i < s.Length; i++) 64 { 65 int a = s[i] - '0';//s[i]中是数字的char,所以-'0'就是转成对应的数字,如 '1'-'0'就是得到1 66 sum = sum * 10 + a; 67 68 } 69 Console.WriteLine(sum); 70 } 71 } 72 }