Hello !
I'm currently working on a school project where at one place in the project I'm suppost to make a program, which converts a binary ip adress to decimal.
The input format is either: 11111111 11110000 00000000 01111111
or 11111111111100000000000001111111
My main program, works as follow:
string iPAddress = "11111111111100000000000001111111";
Console.WriteLine(Datamaskin.IPAddressBinToDec(iPAddress));
Console.ReadKey();
Where the methods is:
static public string IPAddressBinToDec(string iPAddress)
{
string newiPAddress = ReWrite(iPAddress);
return String.Join(".", (newiPAddress.Split('.').Select(x => Convert.ToInt32(x,2))));
}
static public string ReWrite(string input)
{
return Regex.Replace(input, "(.{8})", "$1.").Trim();
}
My problem was how i would handle if the input format was binary with spaces?: 11111111 11110000 00000000 01111111
Thanks for the help.
Asotop
I also had problems validating wheither the input was in correct format or not, any tips there?