user-0 提问: 请教如何将 0x00  0x0F  0xAE 0xF9     按如下图规则 转化成时间 ，c语言代码如何写？

回复:
user: user-1
content: #include <stdio.h> struct DATE {     unsigned int DayOfMonth  : 5;     unsigned int Month       : 4;     unsigned int Year        :12;     unsigned int Spares      :11; } *pD; struct TIME {     unsigned int Milliseconds:10;     unsigned int Seconds     : 6;     unsigned int Minutes     : 6;     unsigned int Hours       : 5;     unsigned int Spares      : 5; } *pT; unsigned char s[8]={0x00,0x0F,0xAE,0xF9,0x04,0x6D,0x00,0x00}; unsigned char d[8];  int main() {     for (int i=0;i<8;i++) d[i]=s[8-1-i];      pD=(struct DATE *)&d[4];     printf("%04u-%02u-%02u\n",         pD->Year        ,         pD->Month       ,         pD->DayOfMonth  );      pT=(struct TIME *)&d[0];     printf("%02u:%02u:%02u.%03u\n",         pT->Hours       ,         pT->Minutes     ,         pT->Seconds     ,         pT->Milliseconds);      return 0; } //2007-07-25 //17:45:00.000 //

user: user-1
content: #include <stdio.h> struct DATE {     unsigned int DayOfMonth  : 5;//bit04～bit00     unsigned int Month       : 4;//bit08～bit05     unsigned int Year        :12;//bit20～bit09     unsigned int Spares      :11;//bit31～bit21 } *pD; struct TIME {     unsigned int Milliseconds:10;//bit09～bit00     unsigned int Seconds     : 6;//bit15～bit10     unsigned int Minutes     : 6;//bit21～bit16     unsigned int Hours       : 5;//bit26～bit22     unsigned int Spares      : 5;//bit31～bit27 } *pT; unsigned char s[8]={0x00,0x0F,0xAE,0xF9,0x04,0x6D,0x00,0x00}; // bbbbbbbb bbbbbbbb bbbbbbbb bbbbbbbb // iiiiiiii iiiiiiii iiiiiiii iiiiiiii // tttttttt tttttttt tttttttt tttttttt // 33222222 22221111 11111100 00000000 // 10987654 32109876 54321098 76543210 // :::::::: :::::::: :::::::: :::::::: // //             YYYYY YYYYYYYM MMMDDDDD // 00000000 00001111 10101110 11111001  0x00,0x0F,0xAE,0xF9 //                      2007    7   25 // //      hhh hhmmmmmm ssssssii iiiiiiii // 00000100 01101011 00000000 00000000  0x04,0x6D,0x00,0x00 //          17    45     00        000 unsigned char d[8]; int main() {     for (int i=0;i<4;i++) {         d[  i]=s[  4-1-i];         d[4+i]=s[4+4-1-i];     }      pD=(struct DATE *)&d[0];     printf("%04u-%02u-%02u\n",         pD->Year        ,         pD->Month       ,         pD->DayOfMonth  );      pT=(struct TIME *)&d[4];     printf("%02u:%02u:%02u.%03u\n",         pT->Hours       ,         pT->Minutes     ,         pT->Seconds     ,         pT->Milliseconds);     return 0; } //2007-07-25 //17:45:00.000 //

user: user-0
content: 特别感谢  特别感谢

user: user-2
content: #include <stdio.h> #include <string.h>  struct date_info {     unsigned int day:5;     unsigned int month:4;     unsigned int year:12;     unsigned int reverse:11; };  int main(void) {     unsigned int hex = 0x000FAEF9;  #if 0     int day = hex & (0x00ffu >> 3);     int month = (hex >> 5) & (0x00ffu >> 4);     int year = (hex >> 9) & (0x00ffffff);      printf("%d.%d.%d\n", year, month, day); #else     struct date_info date;       memcpy(&date, &hex, sizeof(hex));     printf("%lu\n", sizeof(date));     printf("%u\n", date.reverse);     printf("%d.%d.%d\n", date.year, date.month, date.day);  #endif     return 0; }  供参考～

user: user-2
content: #include <stdio.h>   int main(void) {     unsigned int hex = 0x000FAEF9;      int day = hex & (0x00ffu >> 3);     int month = (hex >> 5) & (0x00ffu >> 4);     int year = (hex >> 9) & (0x00ffffff);      printf("%d.%d.%d\n", year, month, day);      return 0; } 供参考～

user: user-0
content: 我自己找的办法。不知道各位 还有没有更好的计算办法  test1.cpp  #include <stdio.h>  #include <stdlib.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h>  #include <sys/time.h> #include <math.h>  int main()  {      char a[17];     strcpy(a, "011111010111");        int len, i, sum = 0, m, j;      len = strlen(a);      if (len <= 16)      {          for (i = 0; i < len; i++)          {              m = 1;              if (a[i] == '1')              {                  for (j = 1; j <= len - i - 1; j++)                      m *= 2;                  sum += m;             }         }          printf("data=%d\n", sum);     }      return 0; }   data=2007

user: user-3
content: #include <stdio.h> typedef struct { 	unsigned day : 5; 	unsigned month : 4; 	unsigned year : 12; 	unsigned unuse : 11; }SDATE; typedef union { 	unsigned udate; 	SDATE sdate; }DATE; unsigned c2le(unsigned char *ch) { 	unsigned ur = 0; 	int i; 	for (i = 0; i < 4; i++) 	{ 		ur <<= 8; 		ur += ch[i]; 	} 	return ur; } int main() { 	DATE T; 	unsigned char Date[4] = {0x00,0x0f,0xae,0xf9}; 	T.udate = c2le(Date); 	printf("%4u-%02u-%02u\n", T.sdate.year, T.sdate.month, T.sdate.day); 	return 0; }  参考一哈。