C# Program to Convert the Octal String to an Integer Number

Given an octal number as input, we need to write a program to convert the given octal number into equivalent integer. To convert an octal string to an integer, we have to use Convert.ToInt32() function to convert the values.

Examples:

Input : 202
Output : 130

Input : 660
Output : 432

Convert the item to an integer using base value 8 by using a foreach loop .

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}

chevron_right


Output:

81
130
3

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}
}

chevron_right


Output:

73
355
219
391
448



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.