Home » C#.Net

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

C# | printing integer values from an array of octal strings: Here, we are going to learn how to convert an array of strings that contains octal values in integers?
Submitted by IncludeHelp, on February 09, 2019

Converting array of octal strings to integers

Let suppose you have some of the strings (i.e. array of strings) containing octal values like "101", "102", "103", "567", "752" which are equivalent to integer 65, 66, 67, 375, 490.

As we have written in the previous post: convert an octal string to integer, we use Convert.ToInt32() function to convert the values.

We will access each item using a foreach loop, and convert the item to an integer using 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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.