//A.B.C.D <=> A*256^3 + B*256^2 + C*256 + D
// <=> A* 16777216 + B*65536 + C*256 + D
long val = (long.Parse(splitArray[0]) * 16777216) +
(long.Parse(splitArray[1]) * 65536) +
(long.Parse(splitArray[2]) * 256) +
(long.Parse(splitArray[3]));
MessageBox.Show(val.ToString());
long ipRem1, ipRem2, ipRem3;
// Convert the text value as long
long convertValue = long.Parse(longValueTextBox.Text.Trim());
// Re-Calculation
long ipAdd1 = Math.DivRem(convertValue, 16777216, out ipRem1);
long ipAdd2 = Math.DivRem(ipRem1, 65536, out ipRem2);
long ipAdd3 = Math.DivRem(ipRem2, 256, out ipRem3);
// Construct IP address
string ipAddress = ipAdd1.ToString().Trim() + "."
+ ipAdd2.ToString().Trim() + "."
+ ipAdd3.ToString().Trim() + "."
+ ipRem3.ToString().Trim();
MessageBox.Show(convertValue + " ---> " + ipAddress);
/// <summary>
/// Checks the validity.
/// </summary>
/// <param name="values">The values.</param>
/// <returns></returns>
private static bool CheckValidity(IEnumerable<string> values)
{
foreach(var val in values)
{
int tempval;
try
{
tempval = Int32.Parse(val);
}
catch(Exception)
{
return false;
}
if (tempval < 0 || tempval > 255)
{
return false;
}
}
return true;
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)