C 将IP地址从int更改为字符串时出现的问题
我正在尝试将IP地址(当前为IPv4,但可识别IPv6)从int数组格式转换为格式化字符串。我正在Ubuntu Linux上工作 我区分IPv4和IPv6地址,然后尝试将int数组转换为字符串 这是我正在使用的代码:C 将IP地址从int更改为字符串时出现的问题,c,string,network-programming,ipv6,ipv4,C,String,Network Programming,Ipv6,Ipv4,我正在尝试将IP地址(当前为IPv4,但可识别IPv6)从int数组格式转换为格式化字符串。我正在Ubuntu Linux上工作 我区分IPv4和IPv6地址,然后尝试将int数组转换为字符串 这是我正在使用的代码: #include <stdio.h> #include <string.h> #include <arpa/inet.h> int main(void) { int i; unsigned int val32; int
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
int main(void)
{
int i;
unsigned int val32;
int ma_ip[4];
char m_ip[4];
char ipaddr[INET_ADDRSTRLEN], ipv6[INET6_ADDRSTRLEN];
int no = 0;
char *token;
char s3[] = "10.1.35.1";
/* just example. test only one ip address.. */
for(i = 0; i < 1; i++)
{
char *mm = strstr(s3, ":");
if( *mm != NULL)
{
token = strtok(s3, ":");
while(token != NULL)
{
token = strtok(NULL, ":");
no++;
}
if(no >= 2 && no <= 7)
printf("\nthis is ipv6\n");
else
printf("\nwrong ipv6\n");
}
else
{
token = strtok(s3, ".");
while(token != NULL)
{
token = strtok(NULL, ".");
no++;
}
if(no == 4)
{
printf("\nthis is ipv4.\n");
val32 = inet_addr(s3)
ma_ip[i] = val32;
}
else
printf("\nwrong ipv4.\n")
}
inet_ntop(AF_INET,&ma_ip[0],ipaddr,INET_ADDRSTRLEN);
printf("\nipaddr = %s\n", ipaddr);
strcpy(&m_ip[0], ipaddr);
printf("\nafter strcpy = %s\n", m_ip[0]);
}
}
实际上应该是:
ipaddr = 10.1.35.1
在strcpy
之后的最后一次printf
中,我也得到了这个错误:
格式“%s”要求参数类型为“char*”,但参数2的类型为“int”错误!**
为什么会发生这种情况,以及如何修复它?
m\u ip[0]
指向第一个元素的位置。如果您希望打印字符串,请尝试使用m_ip
即数组名称或&m_ip[0]
,它基本上传递第一个元素的地址,该地址可以解释为指针
,并基本上导致分段错误。使用m_ip
进行打印(但仍仅0.0.0.10
)
程序中的另一点是,在执行strstr
操作后,应将mm
与NULL
进行比较
对于网络地址操作,我建议您查看一下。我对您的程序有一定的怀疑。 您正在转换ip字符串吗
char s3[] = "10.1.35.1";
转换为整数,再转换为字符串
我想这就是你需要的
struct sockaddr_in sa;
char str[INET_ADDRSTRLEN];
// store this IP address in sa:
inet_pton(AF_INET, "192.0.2.33", &(sa.sin_addr));
// now get it back and print it
inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);
printf("%s\n", str); // prints "192.0.2.33"
关于strcpy之后的最后一个printf,应该是
printf("\nafter strcpy = %s\n", m_ip);
这是一个可行的版本。代码的问题是,strtok()正在修剪字符串“s3”。当n==4时,字符串只剩下“10”
#包括
#包括
#包括
#包括
内部主(空)
{
int i;
无符号int-val32;
国际货币基金组织[4];
charm_ip[4];
字符ipaddr[INET_ADDRSTRLEN],ipv6[INET6_ADDRSTRLEN];
int no=0;
字符*令牌;
字符s3[]=“10.1.35.1”;
char*s4=malloc(strlen(s3)+1);
strcpy(s4,s3);//您可以保存原始字符串。
/*举个例子,只测试一个ip地址*/
对于(i=0;i<1;i++)
{
char*mm=strstrstr(s3,“:”);
如果(mm!=NULL)
{
令牌=strtok(s3,“:”);
while(令牌!=NULL)
{
令牌=strtok(空,“:”);
否++;
}
如果(no>=2&&no而不是inet\u pton
和inet\u ntop
,那么最好使用getaddrinfo()
和getnameinfo()
。我想要更详细的解决方案。谢谢你的回答。strcpy(&m\u ip[0],ipaddr);
是非常错误的。不要使用strcpy
,使用strcpy
char[4]
显然不够大,无法容纳格式化的ip地址字符串。是的,你是对的。我刚刚注意到代码的strtok()问题,并对其进行了快速更改。没有注意到strcpy()问题。谢谢。
printf("\nafter strcpy = %s\n", m_ip);
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <malloc.h>
int main(void)
{
int i;
unsigned int val32;
int ma_ip[4];
char m_ip[4];
char ipaddr[INET_ADDRSTRLEN], ipv6[INET6_ADDRSTRLEN];
int no = 0;
char *token;
char s3[] = "10.1.35.1";
char *s4 = malloc(strlen(s3)+1);
strcpy(s4, s3); //You can save the original string.
/* just example. test only one ip address.. */
for(i = 0; i < 1; i++)
{
char *mm = strstr(s3, ":");
if( mm != NULL)
{
token = strtok(s3, ":");
while(token != NULL)
{
token = strtok(NULL, ":");
no++;
}
if(no >= 2 && no <= 7)
printf("\nthis is ipv6\n");
else
printf("\nwrong ipv6\n");
}
else
{
token = strtok(s3, ".");
while(token != NULL)
{
token = strtok(NULL, ".");
no++;
}
if(no == 4)
{
printf("\nthis is ipv4.\n");
val32 = inet_addr(s4); //use the intact string s4, instead of s3.
ma_ip[i] = val32;
}
else
printf("\nwrong ipv4.\n");
}
inet_ntop(AF_INET,&ma_ip[0],ipaddr,INET_ADDRSTRLEN);
printf("\nipaddr = %s\n", ipaddr);
strcpy(&m_ip[0], ipaddr);
printf("\nafter strcpy = %s\n", m_ip);
}
}