Splitting an IP Address String

Input string was not in a correct format.

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 4482 Views - Last Post: 14 October 2010 - 06:54 AM Rate Topic: -----

#1 cheeky01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-February 10

Splitting an IP Address String

Posted 13 October 2010 - 11:08 AM

hi, im trying to split an IP address and mask into individual octets using the string split method, which works correctly, but when i come to convert from string to an integer (int32) and store it in an integer array i get the error message saying Input string was not in a correct format. Please can someone help


            /*For each octet in the IP address*/
            for (int i = 0; i < IPArray.Length; i++)
            {
                /*Convert the octet from string to an integer and store in an integer array*/
                numIPArray[i] = Int32.Parse(IPArray[i]);
            }
            /*For every octet in the mask*/
            for (int i = 0; i < maskArray.Length; i++)
            {
                /*Convert from string to an integer and store in an integer array*/
                numMaskArray[i] = Int32.Parse(maskArray[i]);
            }



Is This A Good Question/Topic? 0
  • +

Replies To: Splitting an IP Address String

#2 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 11:26 AM

Have you tried using this:

Convert.ToInt32(IPArray[i]);

Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 11:47 AM

I'd have to see the string you're splitting. Here's the basic idea:

string ip = "192.168.0.1";
string[] octets = ip.Split('.');
int[] ipArray = new int[4];
for (int i = 0; i < octets.Length; i++)
    ipArray[i] = int.Parse(octets[i]);



This is tested and works.
Was This Post Helpful? 0
  • +
  • -

#4 cheeky01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-February 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 12:01 PM

Hi, ive tried


 numIPArray[i] = Convert.ToInt32(IPArray[i]);



and it is still giving the same error message, that the input string was not in a correct format.

The string im splitting is just any valid ip address which is entered by a user.
Was This Post Helpful? 0
  • +
  • -

#5 cheeky01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-February 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 12:12 PM

View Postmouse88, on 13 October 2010 - 10:26 AM, said:

Have you tried using this:

Convert.ToInt32(IPArray[i]);



Hi, ive tried

numIPArray[i] = Convert.ToInt32(IPArray[i]);



and it is still giving the same error message, that the input string was not in a correct format.

The string im splitting is just any valid ip address which is entered by a user.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 12:19 PM

You'll have to show us the code where you're actually splitting the string.
Was This Post Helpful? 0
  • +
  • -

#7 cheeky01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-February 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 12:24 PM

View PostinsertAlias, on 13 October 2010 - 11:19 AM, said:

You'll have to show us the code where you're actually splitting the string.



here is all of code up to the error

private byte[] retrieveNetworkAddress(string IPaddr, string mask)
        {
            
            string[] maskArray = new string[4];
            string[] IPArray = new string[4];
            int[] numMaskArray = new int[4];
            int[] numIPArray = new int[4];
            char[] splitter = { '.' };
            int checkOctet = 0;
            int checkOctetIndex = 0;
            int blockSize = 0;
            bool flagOctet = false;
            int octetToChange = 0;
            int replaceOctet = 0;
            string netAddressString = "";
            byte[] netAddress = new byte[4];
            maskArray = mask.Split(splitter, 4);
            IPArray = IPaddr.Split(splitter, 4);
            for (int i = 0; i < IPArray.Length; i++)
            {
                numIPArray[i] = Convert.ToInt32(IPArray[i]);
            }
            for (int i = 0; i < maskArray.Length; i++)
            {
                numMaskArray[i] = Int32.Parse(maskArray[i]);
            }
            for (int i = 0; i < numMaskArray.Length; i++)


Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Splitting an IP Address String

Posted 13 October 2010 - 12:39 PM

You could just use IPAddress...
System.Net.IPAddress ipAddr = System.Net.IPAddress.Parse("192.168.0.50");


Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 12:49 PM

Your code works fine for me. Your input must be incorrect. Console.WriteLine the strings, or MessageBox.Show them to check to make sure they're proper IP addresses: "XXX.XXX.XXX.XXX"
Was This Post Helpful? 0
  • +
  • -

#10 cheeky01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-February 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 01:32 PM

View PostinsertAlias, on 13 October 2010 - 11:49 AM, said:

Your code works fine for me. Your input must be incorrect. Console.WriteLine the strings, or MessageBox.Show them to check to make sure they're proper IP addresses: "XXX.XXX.XXX.XXX"


ive tried using a number of different ip addresses and still get the same problem, i cant understand how its working fine for you, as the address shows fine when i show the strings in the message box, it just seems to be a conversion problem when converting the octet from string to an integer and store in an integer array
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Splitting an IP Address String

Posted 13 October 2010 - 01:48 PM

Can you provide examples of the input you're providing to this function? Have you stepped through the code in the debugger?
Was This Post Helpful? 0
  • +
  • -

#12 cheeky01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-February 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 02:05 PM

View PostJackOfAllTrades, on 13 October 2010 - 12:48 PM, said:

Can you provide examples of the input you're providing to this function? Have you stepped through the code in the debugger?


ip address 169.254.205.136 with a subnet mask of 255.255.0.0

or ip address 192.168.234.234 with a subnet mask of 255.255.255.0

yes i have stepped through the code and the only problem being thrown is the input string was not in a correct format
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 02:20 PM

Well, you haven't shown us the entire method. Are you sure your exception is happening at that point?

Here's what I tried. It's the code you provided, slightly modified to fit out of context:

class Program
{
    static void Main()
    {
        string mask = "255.255.0.0";
        string ip = "169.254.205.136";
        int[] ipArray = GetIpArray(ip, mask);
        int[] maskArray = GetMaskArray(ip, mask);
        foreach (int i in ipArray)
            Console.WriteLine(i.ToString());
        foreach (int i in maskArray)
            Console.WriteLine(i.ToString());
        Console.ReadKey();
    }

    private static int[] GetIpArray(string IPaddr, string mask)
    {

        string[] maskArray = new string[4];
        string[] IPArray = new string[4];
        int[] numMaskArray = new int[4];
        int[] numIPArray = new int[4];
        char[] splitter = { '.' };
        int checkOctet = 0;
        int checkOctetIndex = 0;
        int blockSize = 0;
        bool flagOctet = false;
        int octetToChange = 0;
        int replaceOctet = 0;
        string netAddressString = "";
        byte[] netAddress = new byte[4];
        maskArray = mask.Split(splitter, 4);
        IPArray = IPaddr.Split(splitter, 4);
        for (int i = 0; i < IPArray.Length; i++)
        {
            numIPArray[i] = Convert.ToInt32(IPArray[i]);
        }
        for (int i = 0; i < maskArray.Length; i++)
        {
            numMaskArray[i] = Int32.Parse(maskArray[i]);
        }
        return numIPArray;
        //return numMaskArray;
    }

    private static int[] GetMaskArray(string IPaddr, string mask)
    {

        string[] maskArray = new string[4];
        string[] IPArray = new string[4];
        int[] numMaskArray = new int[4];
        int[] numIPArray = new int[4];
        char[] splitter = { '.' };
        int checkOctet = 0;
        int checkOctetIndex = 0;
        int blockSize = 0;
        bool flagOctet = false;
        int octetToChange = 0;
        int replaceOctet = 0;
        string netAddressString = "";
        byte[] netAddress = new byte[4];
        maskArray = mask.Split(splitter, 4);
        IPArray = IPaddr.Split(splitter, 4);
        for (int i = 0; i < IPArray.Length; i++)
        {
            numIPArray[i] = Convert.ToInt32(IPArray[i]);
        }
        for (int i = 0; i < maskArray.Length; i++)
        {
            numMaskArray[i] = Int32.Parse(maskArray[i]);
        }
        //return numIPArray;
        return numMaskArray;
    }
}


This is the output it produces:

Quote

169
254
205
136
255
255
0
0


Obviously, somehow you're getting something other than a numeric string in there. Here's what I did. I changed one of the values to "X" and re-ran the program. And of course, I got an exception:

Posted Image

Note that I hovered over maskArray and expanded it, and it shows me the realtime values. Do that, and see what they are. It will show you what you're actually trying to convert.

If you do that, and take a screenshot, it should really help us determine better what your issue is.

This post has been edited by insertAlias: 13 October 2010 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#14 cheeky01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-February 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 02:49 PM

ive done what u said and when i hovered over the maskArray and expanded it, it showen me the correct values, but when i did the same for the IPArray i got "[0]" i just need to find out how to add a screen shot and will display it.

This post has been edited by JackOfAllTrades: 13 October 2010 - 03:15 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#15 cheeky01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-February 10

Re: Splitting an IP Address String

Posted 13 October 2010 - 03:07 PM

Attached Image

Attached image(s)

  • Attached Image

This post has been edited by JackOfAllTrades: 13 October 2010 - 03:15 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2