IP地址在数据库中的存储解决方案
规则用“.”把ip分成4个数字 a1,a2,a3,a4 然后把 a1*256*256*256+a2*256*256+a3*256+a4存入数据库
//存入数据库
//存入数据库
public long StrToFlo(string str)
{
char[] sp ={ '.' };
string[] a = str.Split(sp);
long b = 0;
int ii = 256;
for (int i = 0, n = a.Length - 1; i <= a.Length - 1; i++, n--)
b += (long)(Convert.ToInt32(a[i]) * Math.Pow(ii, n));
return b;
}
{
char[] sp ={ '.' };
string[] a = str.Split(sp);
long b = 0;
int ii = 256;
for (int i = 0, n = a.Length - 1; i <= a.Length - 1; i++, n--)
b += (long)(Convert.ToInt32(a[i]) * Math.Pow(ii, n));
return b;
}
// 读出后转换为ip
public string FloToString(long flo)
{
string sp = "";
string b = "";
int i = 256;
for (int n = 3; n >= 0; n--)
{
b += (sp + Convert.ToString(flo/(long)Math.Pow(i, n)));
flo %= (long)Math.Pow(i, n);
sp = ".";
}
return b;
}
{
string sp = "";
string b = "";
int i = 256;
for (int n = 3; n >= 0; n--)
{
b += (sp + Convert.ToString(flo/(long)Math.Pow(i, n)));
flo %= (long)Math.Pow(i, n);
sp = ".";
}
return b;
}