/*
author : ez
date : 2015/7/11
describe : Convert IP string into IP numeric
*/
#include <stdlib.h>
#include <string.h>
int ipstr_to_numeric (const char* _str, int * _addr) {
const char* index;
unsigned char* addr = (unsigned char*) _addr;
int isnumeric = 1;
int i = 3;
*_addr = 0;
index = _str;
while ((*index) && (isnumeric)) {
if (isdigit ((unsigned char) *index))
addr [i] = addr [i] * 10 + (*index - '0'); // big-endian
else if (*index == '.') {
i --;
if (i == -1) isnumeric = 0;
} else
isnumeric = 0;
index ++;
}
if (isnumeric && i) return -1; // error
if (isnumeric) return 0; // successful
}
参数:
@1 = ip C string
@2 = return numerical ip address
返回值:
成功 —— 0
失败 —— -1