首页 > 其他 > 详细

强制类型转换中的精度丢失

时间:2015-04-23 19:24:41      阅读:182      评论:0      收藏:0      [点我收藏+]

  

/*************************************************************************
> File Name: hex2add.c
> Author: cat
> Mail: luojinfu1988@163.com

************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
if (argc != 2) {
printf("usage: %s <addr>\n", argv[0]);
exit(-1);
}

unsigned int temp = (unsigned int) strtol(argv[1], NULL, 16);

struct in_addr addr;
addr.s_addr = htonl(temp);
printf("the ip address is %s\n", inet_ntoa(addr));

return 0;
}

技术分享

/*************************************************************************
> File Name: hex2add.c
> Author: cat
> Mail: luojinfu1988@163.com 

************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
if (argc != 2) {
printf("usage: %s <addr>\n", argv[0]);
exit(-1);
}

unsigned int temp = (unsigned int) strtoll(argv[1], NULL, 16);

struct in_addr addr;
addr.s_addr = htonl(temp);
printf("the ip address is %s\n", inet_ntoa(addr));

return 0;
}

 

技术分享

 

 

技术分享

强制类型转换中的精度丢失

原文:http://www.cnblogs.com/chenchenluo/p/4451096.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!