📜  C#程序将八进制字符串转换为整数

📅  最后修改于: 2021-05-29 18:56:42             🧑  作者: Mango

给定一个八进制数作为输入,我们需要编写一个程序将给定的八进制数转换为等效的整数。要将八进制字符串转换为整数,我们必须使用Convert.ToInt32()函数转换值。

例子:

Input : 202
Output : 130

Input : 660
Output : 432

通过使用foreach循环,使用基值8将项目转换为整数。

程序1:

// C# program to convert an array 
// of octal strings to integers
using System;
using System.Text;
  
class Prog {
  
    static void Main(string[] args)
    {
        string[] str = { "121", "202", "003" };
        int num1 = 0;
        try {
  
            // using foreach loop to access each items
            // and converting to integer numbers
            foreach(string item in str)
            {
                num1 = Convert.ToInt32(item, 8);
                Console.WriteLine(num1);
            }
        }
        catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        }
    }
}

输出:

81
130
3

程式2:

// C# program to convert an array 
// of octal strings to integers
using System;
using System.Text;
  
namespace geeks {
  
class Prog {
  
    static void Main(string[] args)
    {
        string[] str = { "111", "543", "333", "607", "700" };
        int num2 = 0;
        try {
  
            // using foreach loop to access each items
            // and converting to integer numbers
            foreach(string item in str)
            {
                num2 = Convert.ToInt32(item, 8);
                Console.WriteLine(num2);
            }
        }
        catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        }
    }
}
}

输出:

73
355
219
391
448