fork download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <time.h>
  4.  
  5.  
  6. int main()
  7. {
  8. /* time_t now = time(0);
  9. struct tm ltm;
  10. localtime_r(&now, &ltm);
  11. uint8_t bTime[4];
  12. uint8_t year = (ltm.tm_year >= 100) ? (ltm.tm_year - 100) : 0;
  13. uint8_t month = ltm.tm_mon + 1; // 1-12
  14. uint8_t day = ltm.tm_mday; // 1-31
  15.  
  16. uint32_t packed = 0;
  17. packed |= (year & 0x7F) << 25; // 7 bit
  18. packed |= (month & 0x0F) << 21; // 4 bit
  19. packed |= (day & 0x1F) << 16; // 5 bit
  20. packed |= (ltm.tm_hour & 0x1F) << 11; // 5 bit
  21. packed |= (ltm.tm_min & 0x3F) << 5; // 6 bit
  22. packed |= (ltm.tm_sec & 0x1F); // 5 bit
  23.  
  24. bTime[0] = (packed >> 24) & 0xFF;
  25. bTime[1] = (packed >> 16) & 0xFF;
  26. bTime[2] = (packed >> 8) & 0xFF;
  27. bTime[3] = packed & 0xFF;
  28.  
  29. std::string HexValue="";
  30. static uint8_t hexValue[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  31.  
  32. for(int i=0;i<4;i++)
  33. {
  34. HexValue += hexValue[((bTime[i] >> 4) & 0x0F)];
  35. HexValue += hexValue[(bTime[i] & 0x0F)];
  36. }
  37.  
  38. std::cout << HexValue;
  39. */
  40. std::string retValue="";
  41. uint8_t bTime[4];
  42. time_t now = time(0);
  43. tm ltm = *localtime(&now);
  44. ltm.tm_mon++;
  45. ltm.tm_year-=100;
  46.  
  47. bTime[0] = ((((ltm.tm_min & 0x03) << 6) | (ltm.tm_sec & 0x3F)) & 0xFF);
  48. bTime[1] = ((((ltm.tm_hour & 0x0F) << 4) | ((ltm.tm_min & 0x3F) >> 2)) & 0xFF);
  49. bTime[2] = (((((ltm.tm_mon & 0x0F) << 6) | ((ltm.tm_mday & 0x1F) << 1) | (ltm.tm_hour & 0x1F) >> 4)) & 0xFF);
  50. bTime[3] = ((((ltm.tm_year & 0x3F) << 2) | ((ltm.tm_mon & 0x0F) >> 2)) & 0xFF);
  51.  
  52.  
  53. static uint8_t hexValue[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  54.  
  55. for(int i=0;i<4;i++)
  56. {
  57. retValue += hexValue[((bTime[i] >> 4) & 0x0F)];
  58. retValue += hexValue[(bTime[i] & 0x0F)];
  59. }
  60. std::cout << retValue;
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0s 5328KB
stdin
3
2 2
1 1
1 1
2 2
1 1
1 2
3 4
1 2 1 2
1 1 1 2
1 1 2 2
stdout
87B1DA68