utils.cpp 5.9 KB

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