Converting IP addresses from char * to uin32_t
我的灵感来自IP地址到整数的转换
我的代码如下:
1 2 3 4 5 | uint32_r parseIPV4string(char * ipAddress){ char ipbytes[4]; sscanf(ipAddress,"%uhh.%uhh.%uhh.%uhh", &ipbytes[3], &ipbytes[2], &ipbytes[1], &ipbytes[0]); return ipbytes[0] | ipbytes[1] << 8 | ipbytes[2] << 16 | ipbytes[3] << 24; } |
实际上是一个精确的副本,但是我的问题是我的IP地址没有正确显示。我正在震惊和敬畏地观看,因为" 129.173.118.0"和" 129.173.31.187"都返回2164260864
有人可以解释发生了什么吗?
也许我使用的解析器不正确,我不确定确切的工作方式,即"%uhh"。对我来说是新手,所以我不知道该return语句中发生了什么。
您的
您可能是说
无论如何该数组都应该是无符号的,否则以后的
另一个问题是,如果您的系统具有32位int,则即使使用
最后,如果系统具有16位int,则
编写该函数的一种可靠方法是避免任何不受欢迎的提升:
1 2 3 4 5 6 7 8 9 | uint32_t parseIPV4string(char const * ipAddress) { unsigned int ip[4]; if ( 4 != sscanf(ipAddress,"%u.%u.%u.%u", &ip[0], &ip[1], &ip[2], &ip[3]) ) return 0; // or some other indicator or error return ipbytes[3] + ipbytes[2] * 0x100 + ipbytes[1] * 0x10000ul + ipbytes[0] * 0x1000000ul; } |
如果您真的想使用