首页 > 技术文章 > 时间格式化输出 / unsigned char 与 char 本质区别?

secondtononewe 2017-04-17 10:24 原文

 1 #include <time.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 int main(void)
 6 {
 7         struct tm *local;
 8         time_t t;
 9         t=time(NULL);
10         local=localtime(&t);
11         printf("Local hour is: %02x\n",local->tm_hour);
12         printf("Local month is: %02x\n",local->tm_mon);
13         printf("Local wday is: %02x\n",local->tm_wday);
14         printf("Local year is: %d\n",local->tm_year);
15         //local=gmtime(&t);
16         //printf("UTC hour is: %d\n",local->tm_hour);
17 
18 
19         printf(asctime(local));
20 
21         unsigned char tmpbuf[8];  //  若是char 类型,2017输出结果则为ffffffe1 07
22         memset(tmpbuf,0,sizeof(tmpbuf));
23 
24         local->tm_year = local->tm_year + 1900;
25 
26         printf("Print the current year is %d \n",local->tm_year);
27 
28         unsigned char year[2];//  若是char 类型,2017输出结果则为ffffffe1 07
29      memset(year,0,sizeof(year));
30       memcpy(year,&local->tm_year,2); //将占4个byte的int型转化成两个byte的char类型输出;



printf("Print the year %02x %02x %02x %02x \n",year[0],year[1],year[0],year[1]);



sprintf(tmpbuf,"%c%c%c%c%c%c%c%c",year[0],year[1],local->tm_mon,local->tm_mday,

local->tm_hour,local->tm_min,local->tm_sec,local->tm_wday);

int i =0;



for(i = 0;i < 8;i++)

{

printf("%02x ",tmpbuf[i]);
}
return 0;
}

 

在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:

#ifndef _TM_DEFINED
struct tm {
          int tm_sec;       /* 秒 – 取值区间为[0,59] */
          int tm_min;       /* 分 - 取值区间为[0,59] */
          int tm_hour;      /* 时 - 取值区间为[0,23] */
          int tm_mday;      /* 一个月中的日期 - 取值区间为[1,31] */
          int tm_mon;       /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
          int tm_year;      /* 年份,其值等于实际年份减去1900 */
          int tm_wday;      /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
          int tm_yday;      /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
          int tm_isdst;     /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
          };
#define _TM_DEFINED
#endif 

 

 

gcc test.c -o test

 

./test执行后,结果:

 

debian:/home/zhangling# ./test
Local hour is: 0a
Local month is: 03
Local wday is: 01
Local year is: 117
Mon Apr 17 10:14:41 2017
Print the current year is 2017
Print the year e1 07 e1 07
e1 07 03 11 0a 0e 29 01

 

但若代码中,

unsigned char tmpbuf[8];//  若是char 类型,2017输出结果则为ffffffe1 07
unsigned char year[2];//  若是char 类型,2017输出结果则为ffffffe1 07

 

char和unsigned char的本质区别:【有待继续加深理解.......................】

首先在内存中,char与unsigned char没有什么不同,都是一个字节,唯一的区别是,char的最高位为符号位,因此char能表示-128~127, unsigned char没有符号位,因此能表示0~255,这个好理解,8个bit,最多256种情况,因此无论如何都能表示256个数字。

在实际使用过程种有什么区别呢?

主要是符号位,但是在普通的赋值,读写文件和网络字节流都没什么区别,反正就是一个字节,不管最高位是什么,最终的读取结果都一样,只是你怎么理解最高位而已,在屏幕上面的显示可能不一样。

但是我们却发现在表示byte时,都用unsigned char,这是为什么呢?

首先我们通常意义上理解,byte没有什么符号位之说,更重要的是如果将byte的值赋给int,long等数据类型时,系统会做一些额外的工作。

如果是char,那么系统认为最高位是符号位,而int可能是16或者32位,那么会对最高位进行扩展(注意,赋给unsigned int也会扩展)

而如果是unsigned char,那么不会扩展。

这就是二者的最大区别。

推荐阅读