long atol_(const char* nptr){
long total = 0;
char sign = '+';
while( isspace( *nptr ) ){ ++nptr; } // 跳过空格
if( *nptr == '-' || *nptr == '+' ){ sign = *nptr++; } // 检查是否指定符号
while( isdigit( *nptr ) ){
total = 10 * total + ( (*nptr++) - '0' );
}
return (sign == '-') ? -total : total;
}
ULONG inet_addr( IN char *cp ){
char ipBytes[4]={0};
LONG i;
for( i=0; i<4; i++, cp++ ){
ipBytes[i] = (char)atol_( cp );
if( !(cp = strchr( cp, '.' )) ){ break; }
}
return *(ULONG*)ipBytes;
}自己实现 atol, inet_addr 源代码
最新推荐文章于 2022-06-08 15:41:27 发布
本文深入解析了C++代码片段,包括如何使用自定义函数longatol_进行长整型数字转换以及如何通过INet_addr函数实现IP地址解析。代码中展示了跳过空格、识别符号、处理数字字符并最终返回转换后的数值或IP地址的过程。
5844

被折叠的 条评论
为什么被折叠?



