utils.cpp 6.1 KB

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