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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.