我得到一个根据IP地址,确定地理位置的源码,里面有 一个方法 是将IP转换成长整型 算法中涉及到了 位移操作,不太理解为什么 这样做 即 Ipv += Ipi << (3 - i) * 8; 这句话 不太理解 代码如下:
// IP字符串->长整型值
public static uint IpStringToInt(string IpString)
{
uint Ipv = 0;
string[] IpStringArray = IpString.Split('.');
int i;
uint Ipi;
for (i = 0; i < 4 && i < IpStringArray.Length; i++)
{
if (IsNumeric(IpStringArray[i]))
{
Ipi = (uint)Math.Abs(Convert.ToInt32(IpStringArray[i]));
if (Ipi > 255) Ipi = 255;
Ipv += Ipi << (3 - i) * 8;
}
}
return Ipv;
}
// 字符串数值型判断
public static bool IsNumeric(string s)
{
if (s != null && System.Text.RegularExpressions.Regex.IsMatch(s, @"^-?\d+$"))
return true;
else
return false;
}
------解决方案--------------------------------------------------------
用十六进制来表达,就是这样:
00 ~ FF
你把它转成 10 进制