//基于C语言的ip地址16进制整型与字符串相转换的简单实现方式
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned int ipv4_str_to_hex(char* str) {
char ip1[4] = "";
char ip2[4] = "";
char ip3[4] = "";
char ip4[4] = "";
char* p = str;
strcpy(ip1, strsep(&p, "."));
strcpy(ip2, strsep(&p, "."));
strcpy(ip3, strsep(&p, "."));
strcpy(ip4, strsep(&p, "."));
unsigned int ip = atoi(ip1);
ip = ip * 256 + atoi(ip2);
ip = ip * 256 + atoi(ip3);
ip = ip * 256 + atoi(ip4);
return ip;
}
void ipv4_hex_to_str(unsigned int iphex, char* ipstr) {
sprintf(ipstr, "%u.%u.%u.%u", (iphex >> 24) & 0xff, (iphex >> 16) & 0xff, (iphex >> 8) & 0xff);
}
int main() {
char ipstr[64] = "3131372E37342E3133362E33343B";
unsigned int a;
a = ipv4_str_to_hex(ipstr);
printf("hex ip:0x%08x\n", a);
char ipstr2[64] = "";
ipv4_hex_to_str(a, ipstr2);
printf("str ip is: %s\n", ipstr2);
return 0;
}
这段代码在vs2019环境下编译报错,提示如下:
错误 LNK2019 无法解析的外部符号 strsep,函数 ipv4_str_to_hex 中引用了该符号 Project1 E:\ota_server_client_0606\Project1\Project1\server_main.obj 1
这是什么原因导致的?