2

I am trying to make a little program, where i can type in an IPv4 address and a SubMask address.

Works fine...

Then I will make an AND, so I can see what SubNetwork the IPv4 address is from, in (binary form)... and i can't get it to work, can someone help..

Here im trying to convert the bitwise string to an Int, so i can make and AND, but my variabels iSIDs and iSIDr (int datatype) dont get a value..

The variabels subresult and result are decimal IPv4 convertet address in 32 binary-form so BitWise strings...

        string SIDsubresult = subresult;
        SIDsubresult = SIDsubresult.Replace(".", "");
        string SIDresult = result;
        SIDresult = SIDresult.Replace(".", "");
        int iSIDs = Convert.ToInt32(SIDsubresult);
        int iSIDr = Convert.ToInt32(SIDresult);
        Console.Write(iSIDr & iSIDs);

EDIT/UPDATE

Just so all have the code before the code i started writing.. My code before then AND code is here

        var split = (from p in ipv4add.Split('.') select int.Parse(p)).ToArray();
        string result = string.Format("{0}.{1}.{2}.{3}",
            Convert.ToString(split[0], 2).PadLeft(8, '0'),
            Convert.ToString(split[1], 2).PadLeft(8, '0'),
            Convert.ToString(split[2], 2).PadLeft(8, '0'),
            Convert.ToString(split[3], 2).PadLeft(8, '0'));

        var subsplit = (from s in subnetmaske.Split('.') select int.Parse(s)).ToArray();
        string subresult = string.Format("{0}.{1}.{2}.{3}",
            Convert.ToString(subsplit[0], 2).PadLeft(8, '0'),
            Convert.ToString(subsplit[1], 2).PadLeft(8, '0'),
            Convert.ToString(subsplit[2], 2).PadLeft(8, '0'),
            Convert.ToString(subsplit[3], 2).PadLeft(8, '0'));

        string SIDsubresult = subresult;
        SIDsubresult = SIDsubresult.Replace(".", "");
        string SIDresult = result;
        SIDresult = SIDresult.Replace(".", "");
        int iSIDs = Convert.ToInt32(SIDsubresult);
        int iSIDr = Convert.ToInt32(SIDresult);
        Console.Write(iSIDr & iSIDs);
Thomas BP
  • 1,187
  • 3
  • 26
  • 62
  • 2
    Converting an IPv4 address to int by parsing it without periods is very wrong and usually overflows – harold Mar 08 '17 at 10:12
  • See for example here http://stackoverflow.com/questions/1499269/how-to-check-if-an-ip-address-is-within-a-particular-subnet on how to check an IP for a subnet – xanatos Mar 08 '17 at 10:14
  • Use uint instead of int. IPV4 max values can be 0xFFFFFFFF which will overflow an int, but not uint. – jdweng Mar 08 '17 at 10:29

2 Answers2

0

its because int max have 9 numbers length and your ip add is longer Try work with long:

string SIDsubresult = subresult;
    SIDsubresult = SIDsubresult.Replace(".", "");
    string SIDresult = result;
    SIDresult = SIDresult.Replace(".", "");
    long iSIDs = Convert.ToInt64(SIDsubresult);
    long iSIDr = Convert.ToInt64(SIDresult);
    Console.Write(iSIDr & iSIDs);
  • 1
    Still really wrong though, the overflow is not the principal problem, but for ex. "1.1.1.1" must parse to 0x01010101 not to (decimal) 1111 (aka 0x457 which just looks like random crap because it is). – harold Mar 08 '17 at 10:32
0

I tried this and work for me:

 var sidsub = SIDsubresult.Split('.');
        var sid = SIDresult.Split('.');

        var result = "";
        for (int i = 0; i < sidsub.Length; i++)
        {
            result += (int.Parse(sidsub[i]) & int.Parse(sid[i]));
            if (i + 1 != sidsub.Length) result += ".";
        }