Asiful Haque Mahfuze
BAN USERLike C#
public static bool IsValidIp(string address)
{
int dotCount = 0;
int returnValue = 0;
var isValid = true;
var total = 0;
foreach (var digit in address)
{
if ((digit < '0' || digit > '9') && digit != '.' || !isValid)
return false;
if (digit == '.')
{
total += returnValue;
returnValue = 0;
dotCount++;
continue;
}
returnValue = (returnValue * 10) + (digit - '0');
isValid &= returnValue < 256;
}
isValid &= (total != 0) && (total <= (255 * 4)) && dotCount == 3;
return isValid;
}
public static bool IsValidIp(string address)
{
int dotCount = 0;
int returnValue = 0;
var isValid = true;
var total = 0;
foreach (var digit in address)
{
if ((digit < '0' || digit > '9') && digit != '.' || !isValid)
return false;
if (digit == '.')
{
total += returnValue;
returnValue = 0;
dotCount++;
continue;
}
returnValue = (returnValue * 10) + (digit - '0');
isValid &= returnValue < 256;
}
isValid &= (total != 0) && (total <= (255 * 4)) && dotCount == 3;
return isValid;
}
public static int ConvertToInteger(string number)
{
int sign = 1;
int returnValue = 0;
foreach (var digit in number)
{
if (digit == '+') continue;
if (digit == '-')
{
sign = -1;
continue;
}
if (digit < '0' || digit > '9')
throw new ArgumentException("String cannot have numbers!");
returnValue = (returnValue * 10) + (digit - '0');
}
return returnValue * sign;
}
- Asiful Haque Mahfuze February 10, 2015