C# arrays

Can i convert arrays of type string to int?

Page 1 of 1

4 Replies - 684 Views - Last Post: 13 September 2010 - 04:00 AM Rate Topic: -----

#1 yosuke06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-May 10

C# arrays

Posted 12 September 2010 - 07:00 AM

..user must enter a string of numbers and i split
that string to get an array of strings..

how can i convert string[] to int[] ?
Is This A Good Question/Topic? 0
  • +

Replies To: C# arrays

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: C# arrays

Posted 12 September 2010 - 07:10 AM

http://www.asciitable.com/

Take particular note of the range of characters between decimal 48 and 57. Use that information to do what you are attempting.
Was This Post Helpful? 0
  • +
  • -

#3 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: C# arrays

Posted 13 September 2010 - 12:51 AM

View Postyosuke06, on 12 September 2010 - 06:00 AM, said:

..user must enter a string of numbers and i split
that string to get an array of strings..

how can i convert string[] to int[] ?


Welcome on board, yosuke06!
Try these code snips
(Next time you need to explain your
task more widely for the better understanding
your problem)
//convert array of letters to array of integers (ascii codes)
            string[] sarr = new string[] {"A","B","C","D","E","F","G" ,"H" };
            int cnt = sarr.Length;
            int[] iarr = new int[cnt];
            for (int i = 0; i < cnt; i++)
            {
                char c = sarr[i].ToCharArray()[0];
                iarr[i] = (int)c;
                Console.WriteLine("Letter: {0}\tNumber: {1}", sarr[i], iarr[i]);
            }
           
        }


...and this one
        //convert numeric strings to array of integers
                 string[] sarr = new string[] {"101","102","103","104","105","106","107","108" };
            int cnt = sarr.Length;
            int[] iarr = new int[cnt];
            for (int i = 0; i < cnt; i++)
            {

                iarr[i] = Convert.ToInt32(sarr[i]);
                Console.WriteLine("String: {0}\tNumber: {1}", sarr[i], iarr[i]);
            }


~'J'~
Was This Post Helpful? 1
  • +
  • -

#4 yosuke06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-May 10

Re: C# arrays

Posted 13 September 2010 - 03:48 AM

View Postfixo, on 13 September 2010 - 02:51 PM, said:

View Postyosuke06, on 12 September 2010 - 06:00 AM, said:

..user must enter a string of numbers and i split
that string to get an array of strings..

how can i convert string[] to int[] ?


Welcome on board, yosuke06!
Try these code snips
(Next time you need to explain your
task more widely for the better understanding
your problem)
//convert array of letters to array of integers (ascii codes)
            string[] sarr = new string[] {"A","B","C","D","E","F","G" ,"H" };
            int cnt = sarr.Length;
            int[] iarr = new int[cnt];
            for (int i = 0; i < cnt; i++)
            {
                char c = sarr[i].ToCharArray()[0];
                iarr[i] = (int)c;
                Console.WriteLine("Letter: {0}\tNumber: {1}", sarr[i], iarr[i]);
            }
           
        }


...and this one
        //convert numeric strings to array of integers
                 string[] sarr = new string[] {"101","102","103","104","105","106","107","108" };
            int cnt = sarr.Length;
            int[] iarr = new int[cnt];
            for (int i = 0; i < cnt; i++)
            {

                iarr[i] = Convert.ToInt32(sarr[i]);
                Console.WriteLine("String: {0}\tNumber: {1}", sarr[i], iarr[i]);
            }


~'J'~


hey..thanks for the code snips and also for
the tip..i'll make sure to explain my task
much clearer next time..

..the code snips helped a lot'..,

~tnx
Was This Post Helpful? 0
  • +
  • -

#5 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: C# arrays

Posted 13 September 2010 - 04:00 AM

View Postyosuke06, on 13 September 2010 - 02:48 AM, said:

View Postfixo, on 13 September 2010 - 02:51 PM, said:

View Postyosuke06, on 12 September 2010 - 06:00 AM, said:

..user must enter a string of numbers and i split
that string to get an array of strings..

how can i convert string[] to int[] ?


Welcome on board, yosuke06!
Try these code snips
(Next time you need to explain your
task more widely for the better understanding
your problem)
//convert array of letters to array of integers (ascii codes)
            string[] sarr = new string[] {"A","B","C","D","E","F","G" ,"H" };
            int cnt = sarr.Length;
            int[] iarr = new int[cnt];
            for (int i = 0; i < cnt; i++)
            {
                char c = sarr[i].ToCharArray()[0];
                iarr[i] = (int)c;
                Console.WriteLine("Letter: {0}\tNumber: {1}", sarr[i], iarr[i]);
            }
           
        }


...and this one
        //convert numeric strings to array of integers
                 string[] sarr = new string[] {"101","102","103","104","105","106","107","108" };
            int cnt = sarr.Length;
            int[] iarr = new int[cnt];
            for (int i = 0; i < cnt; i++)
            {

                iarr[i] = Convert.ToInt32(sarr[i]);
                Console.WriteLine("String: {0}\tNumber: {1}", sarr[i], iarr[i]);
            }


~'J'~


hey..thanks for the code snips and also for
the tip..i'll make sure to explain my task
much clearer next time..

..the code snips helped a lot'..,

~tnx

I glad to help where I can.
Happy coding :)

~'J'~
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1