码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-23 19:25:12      阅读:159      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!