linux下自己编写的实现inet_ntoa函数的实现(简单实现)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char *my_inet_ntoa(unsigned long);

char str[30];
int main(int argc,char **argv)
{
unsigned long inaddr=3232235777;
printf("%s\n",my_inet_ntoa(inaddr));
return 0;
}

char *my_inet_ntoa(unsigned long inaddr)
{
    int a[4],i;
    char b[20],c[15],d[10],e[5];
    a[0]=((inaddr & 0xff000000)>>24);
    a[1]=((inaddr & 0x00ff0000)>>16);
    a[2]=((inaddr & 0x0000ff00)>>8);
    a[3]=((inaddr & 0x000000ff)>>0);
    
//    itoa(a[0],b,10);
    sprintf(b,"%d",a[0]);
    strcat(b,".");
//    itoa(a[1],c,10);
    sprintf(c,"%d",a[1]);
    strcat(c,".");
//    itoa(a[2],d,10);
    sprintf(d,"%d",a[2]);
    strcat(d,".");
//    itoa(a[3],e,10);
    sprintf(e,"%d",a[3]);
        
    strcat(str,strcat(b,strcat(c,strcat(d,e))));
    return str;
    
}

 

转载于:https://www.cnblogs.com/sherlockhomles/archive/2013/05/24/3096737.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值