utils.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "stdafx.h"
  2. #include "utils.h"
  3. namespace utils
  4. {
  5. const unsigned short CRC16_TABLE[16] = {
  6. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  7. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF
  8. };
  9. std::string& trim(std::string& str)
  10. {
  11. #if _MSC_VER >= 1200 && _MSC_VER < 1600
  12. for (std::string::iterator it = str.begin(); it != str.end();) {
  13. !isspace(*it) ? it++ : it = it = str.erase(it);
  14. }
  15. #elif _MSC_VER >= 1600
  16. str.erase(std::remove_if(str.begin(), str.end(), [](unsigned char x) {return std::isspace(x); }), str.end()); //C++17
  17. #endif
  18. return str;
  19. }
  20. std::string _dprintf(CHAR* pszStr, ...)
  21. {
  22. const int LOGLEN = 1024*100;
  23. static char pszData[LOGLEN] = {0};
  24. memset(pszData, 0, LOGLEN);
  25. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  26. sprintf(pszData, _T("[%s %s]\t"), _T("TCL"), CTime::GetCurrentTime().Format(_T("%H:%M:%S")));
  27. #elif _MSC_VER >= 1500
  28. _stprintf_s(pszData, LOGLEN, _T("[%s %s]\t"), _T("TCL"), CTime::GetCurrentTime().Format(_T("%H:%M:%S")).GetString());
  29. #endif
  30. int len = strlen(pszData);
  31. va_list args;
  32. va_start(args, pszStr);
  33. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  34. _vsnprintf(szData + len, LOGLEN - len, pszStr, args);
  35. #elif _MSC_VER >= 1500
  36. _vsntprintf_s(pszData + len, LOGLEN - len, _TRUNCATE, pszStr, args);
  37. #endif
  38. va_end(args);
  39. if (pszData[strlen(pszData) - 1] != '\n')
  40. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  41. strcat(pszData, "\n");
  42. #elif _MSC_VER >= 1500
  43. strcat_s(pszData, LOGLEN, "\n");
  44. #endif
  45. OutputDebugStringA(pszData);
  46. return std::string(pszData);
  47. }
  48. std::string ByteToChars(byte b)
  49. {
  50. char szhex[3] = { 0 };
  51. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  52. sprintf(szhex, "%02X", b);
  53. #elif _MSC_VER >= 1500
  54. _stprintf_s(szhex, "%02X", b);
  55. #endif
  56. return std::string(szhex);
  57. }
  58. BOOL IsValidString(LPCTSTR lpszString)
  59. {
  60. if (lpszString == NULL)
  61. return FALSE;
  62. do {
  63. // ASCII可显示的字符;
  64. if (*lpszString < 32 || *lpszString > 126) {
  65. return FALSE;
  66. }
  67. } while (*++lpszString);
  68. return TRUE;
  69. }
  70. unsigned char TwoHexCharToInteger(char high, char low)
  71. {
  72. std::string str = "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
  73. if (str.find_first_of(high) == std::string::npos || str.find_first_of(low) == std::string::npos)
  74. {
  75. return 0;
  76. }
  77. char Numb1 = high >= 'A' ? (toupper(high) - '0' - 7) * 16 : (high - '0') * 16;
  78. char Numb2 = low >= 'A' ? (toupper(low) - '0' - 7) : (low - '0');
  79. return (Numb1 + Numb2);
  80. }
  81. std::string BytesToHexString(const unsigned char* pbuffer, int nLen)
  82. {
  83. std::string hex;
  84. char szhex[5] = { 0 };
  85. for (int i = 0; i < nLen; i++)
  86. {
  87. memset(szhex, 0, 5);
  88. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  89. sprintf(szhex, "%02X", pbuffer[i]);
  90. #elif _MSC_VER >= 1500
  91. _stprintf_s(szhex, "%02X", pbuffer[i]);
  92. #endif
  93. hex.append(szhex);
  94. }
  95. return hex;
  96. }
  97. std::string BytesToHexString(const unsigned char* pbuffer, int nLen, char chSpace)
  98. {
  99. std::string hex;
  100. char szhex[5] = { 0 };
  101. for (int i = 0; i < nLen; i++)
  102. {
  103. memset(szhex, 0, 5);
  104. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  105. sprintf(szhex, "%02X%c", pbuffer[i], chSpace);
  106. #elif _MSC_VER >= 1500
  107. _stprintf_s(szhex, "%02X%c", pbuffer[i], chSpace);
  108. #endif
  109. hex.append(szhex);
  110. }
  111. return hex.substr(0, hex.size() - 1);
  112. }
  113. std::string HexStringToBytes(std::string strHex, const int& len)
  114. {
  115. byte value = 0;
  116. std::string strBytes;
  117. int nSize = strHex.size();
  118. for (int i = 0; i < nSize; i += len)
  119. {
  120. #if _MSC_VER >=1200 && _MSC_VER < 1500
  121. strBytes.append((char)TwoHexCharToInteger(strHex[i], strHex[i + 1]), 1);
  122. #elif _MSC_VER >=1500
  123. strBytes.push_back((char)TwoHexCharToInteger(strHex[i], strHex[i + 1]));
  124. #endif
  125. }
  126. return strBytes;
  127. }
  128. unsigned short CRC16Calculate(byte* pBuffer, unsigned int wordLength)
  129. {
  130. byte byteTemp(0);
  131. unsigned short wordCRC(0);
  132. wordCRC = 0xFFFF;
  133. while (wordLength--) {
  134. byteTemp = (byte)(wordCRC >> 0x0C);
  135. wordCRC <<= 4;
  136. wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) >> 0x04)];
  137. byteTemp = (byte)(wordCRC >> 0x0C);
  138. wordCRC <<= 4;
  139. wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) & 0x0F)];
  140. pBuffer++;
  141. }
  142. return wordCRC;
  143. }
  144. bool GetResourceData(DWORD dwResourceID, LPCTSTR lpExt, std::string &rtnData)
  145. {
  146. HGLOBAL hGlobal = NULL;
  147. HRSRC hSource = NULL;
  148. LPVOID lpBuffer = NULL;
  149. DWORD dwSize = 0;
  150. BOOL bResult = FALSE;
  151. // 1.查找资源,如果没找到返回Null
  152. hSource = FindResource(NULL, MAKEINTRESOURCE(dwResourceID), lpExt);
  153. if (hSource == NULL) {
  154. _dprintf(_T("查找资源失败:%s,ID=%d"), lpExt, dwResourceID);
  155. return false;
  156. }
  157. // 2.获取资源大小;
  158. dwSize = (UINT)SizeofResource(NULL, hSource);
  159. // 3.加载资源;
  160. hGlobal = LoadResource(NULL, hSource);
  161. if (hGlobal == NULL) {
  162. _dprintf(_T("加载资源失败:%s,ID=%d"), lpExt, dwResourceID);
  163. return false;
  164. }
  165. // 4.提取到buffer中;
  166. lpBuffer = LockResource(hGlobal);
  167. if (lpBuffer == NULL) {
  168. _dprintf(_T("锁定资源失败:%s,ID=%d"), lpExt, dwResourceID);
  169. return false;
  170. }
  171. #if _MSC_VER >=1200 && _MSC_VER < 1500
  172. rtnData = "";
  173. #elif _MSC_VER >= 1500
  174. rtnData.clear();
  175. #endif
  176. rtnData.append((char*)lpBuffer, dwSize);
  177. // 释放资源;
  178. UnlockResource(hGlobal);
  179. FreeResource(hGlobal);
  180. return true;
  181. }
  182. }