如何将IP地址字符串转换为数字数组
最近在做一个项目用到LWIP,通过触摸屏幕上的数字键盘输入要设置的IP地址和网关地址,然后再用输入的地址去设置重新设置lwip。那么问题就来了,输入的IP地址字符串应该怎么去转换成 ip[4] 数组呢?受限于自己的敲BUG水平,终于在网上找到了大佬写的比较简洁且容易理解的代码
这是大佬的原来的代码,包括IPV4,IPV6,以及MAC地址的字符串转数组
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define ipaddr "192.16.100.20" // dotted decimal
#define ip6addr "2019:1255:1232::1:1" // colon hexadecimal
#define macaddr "00-11-22-33-44-FF"int macstr_parse(const char *point, unsigned char result[6]) {for (int i = 0; i < 6; i++) {result[i] = 0xfe;}char buf[18] = {0}, p = 0, q = 0;strcpy(buf, point);buf[strlen(point)] = '-';for(int i = 0;i < 6; i++) {q = strchr(buf+p, '-') - buf;buf[q] = '\0';result[i] = strtol(buf+p, NULL, 16);p = q + 1;}return 1;
}int ip6str_parse(const char *point, unsigned int result[4]) {for (int i = 0; i < 4; i++) {result[i] = 0;}char buf[40] = {0}, revbuf[40], p = 0, q = 0;strcpy(buf, point);strcpy(revbuf, point);strrev(revbuf);buf[strlen(point)] = ':';revbuf[strlen(point)] = ':';for(int i = 0;i < 8; i++) {q = strchr(buf+p, ':') - buf;buf[q] = '\0';if (i % 2 == 0) {result[i/2] = 0;result[i/2] = strtol(buf+p, NULL, 16)<<16;} else {result[i/2] += strtol(buf+p, NULL, 16);}p = q + 1;// if we find ::, then we should scan revbuf.2019:1::1 1::1:2019if (buf[p] == ':') {p = q = 0;for (int j = 7;j > i;j--) {q = strchr(revbuf+p, ':') - revbuf;revbuf[q] = '\0';strrev(revbuf+p);if (j % 2 == 0) {result[j/2] += strtol(revbuf+p, NULL, 16)<<16;} else {result[j/2] = 0;result[j/2] += strtol(revbuf+p, NULL, 16);}p = q + 1;if (revbuf[p] == ':') {break;}}break;}}return 1;
}int macstr_parse(const char *point, unsigned char result[6]) {for (int i = 0; i < 6; i++) {result[i] = 0xfe;}char buf[18] = {0}, p = 0, q = 0;strcpy(buf, point);buf[strlen(point)] = '-';for(int i = 0;i < 6; i++) {q = strchr(buf+p, '-') - buf;buf[q] = '\0';result[i] = strtol(buf+p, NULL, 16);p = q + 1;}return 1;
}
来说一下我的项目,是一个这样的界面
因为我的项目中不仅需要将字符串转换,还需要对输入的字符串进行判断,对于输入有误的地址直接返回错误,防止用错误的IP来设置lwip。所以我对大佬的代码进行了修改,以下是我修改后的代码
/*** @brief 检测IP地址输入是否正确,如果正确就将地址转换数字数组存放在addr中,并返回0,* 如果不正确就返回-1* @param Addr_s IP地址字符串* @param Addr_n 地址数组* @return uint8_t 检查正确返回0, 错误返回1*/
uint8_t addr_check(const char *Addr_s, uint16_t *Addr_n)
{char buf[18] = {0};uint16_t temp;char p_head = 0, p_tail = 0; //字符串头指针和尾指针//先将addr数组里的值清空for (int i = 0; i < 4; i++) {*(Addr_n + i) = 0;}strcpy(buf, Addr_s); //拷贝字符串buf[strlen(Addr_s)] = '.';for(int i=0; i<4; i++){//头指针和尾指针的用法:例如"192.",头指针指向'1',尾指针指向'.'if(buf[p_head] == '\0'){return 1; //如果头指针指向元素为'\0',返回错误}p_tail = strchr(buf+p_head, '.') - buf; //确保地址的每一个数字都有,且不能超过3位if((p_tail-p_head) == 0 || (p_tail-p_head) > 3){ return 1;}//将尾指针指向的'.'替换为'\0',用atoi函数转换成数字buf[p_tail] = '\0';temp = strtol(buf+p_head, NULL, 10);if(temp > 255){ //如果地址值大于255,返回错误return 1;}*(Addr_n + i) = temp;//头指针指向下一位p_head = p_tail + 1;}return 0;
}
参考文章链接:<https://blog.csdn.net/z7________/article/details/107796029