uint8_t StrToIP(const int8_t* str, void *ip)
{
/* The count of the number of bytes processed. */
int i;
/* A pointer to the next digit to process. */
const char * start;
start = str; // 字符串指针赋值
// IPV4
//---------------------------------------------------
for (i = 0; i < 4; i++) // 一共四个十进制字符串
{
/* The digit being processed. */
char c;
/* The value of this byte. */
int n = 0;
while (1)
{
c = * start; // 字符串某字符赋值
start++; // 从前往后(从左向右)
if (c >= '0' && c <= '9') // 在“0~9”范围内
{
n *= 10; // 权重
n += c - '0'; // 将'0'~'9'字符转换为对应的数字
}
/* We insist on stopping at "." if we are still parsing
the first, second, or third numbers. If we have reached
the end of the numbers, we will allow any character. */
else if ((i < 3 && c == '.') || i == 3)
{
break; // 遇到'.'则解析下一十进制字符串
}
else
{
return 0; // 解析失败
}
}
if (n >= 256) // n过大,解析失败
{
return 0;
}
((uint8_t*)ip)[i] = n; // 赋值给IP数组
}
return 1; // 解析成功,返回1
}