Print integer values from an array of strings containing octal values in C#

C# printing integer values from a variety of octal strings: Here, we will figure out how to change over a variety of strings that contains octal values in integers?

Changing over an exhibit of octal strings to integers

Let guess you have a portion of the strings (for example exhibit of strings) containing octal values like “101”, “102”, “103”, “567”, “752” which are proportionate to integer 65, 66, 67, 375, 490.

As we have written in the past post: convert an octal string to an integer, we use Convert.ToInt32() capacity to change over the values.

We will get to everything utilizing a foreach circle, and convert the thing to an integer utilizing base value 8.

Code:

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = { "101", "102", "103", "567", "752"};
            int num = 0;
            try
            {
                //using foreach loop to access each items
                //and converting to integer numbers 
                foreach (string item in str)
                {
                    num = Convert.ToInt32(item, 8);
                    Console.WriteLine(num);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output:

65
66
67
375
490

Leave a Comment

error: Alert: Content is protected!!