CharConvert.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "stdafx.h"
  2. #include "CharConvert.h"
  3. /* string to utf8 */
  4. std::string stringToUTF8(const std::string& str)
  5. {
  6. int nwLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
  7. wchar_t* pwBuf = new wchar_t[nwLen + 1];
  8. ZeroMemory(pwBuf, nwLen * 2 + 2);
  9. MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
  10. int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
  11. char* pBuf = new char[nLen + 1];
  12. ZeroMemory(pBuf, nLen + 1);
  13. WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
  14. std::string retStr(pBuf);
  15. delete[] pwBuf;
  16. delete[] pBuf;
  17. pwBuf = NULL;
  18. pBuf = NULL;
  19. return(retStr);
  20. }
  21. /* utf8 to string */
  22. std::string UTF8Tostring(const std::string& str)
  23. {
  24. int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
  25. wchar_t* pwBuf = new wchar_t[nwLen + 1];
  26. memset(pwBuf, 0, nwLen * 2 + 2);
  27. MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);
  28. int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
  29. char* pBuf = new char[nLen + 1];
  30. memset(pBuf, 0, nLen + 1);
  31. WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
  32. std::string retStr = pBuf;
  33. delete[] pwBuf;
  34. delete[] pBuf;
  35. pwBuf = NULL;
  36. pBuf = NULL;
  37. return(retStr);
  38. }
  39. /* UTF8 to URL String */
  40. std::string UTF8ToURLString(const std::string& str)
  41. {
  42. std::string strRes;
  43. unsigned int iIndex = 0, iCount = str.length();
  44. while(iIndex < iCount)
  45. {
  46. if((unsigned char)str.c_str()[iIndex] < 0x7F)
  47. {
  48. if ((unsigned char)str.c_str()[iIndex] == (unsigned char)(' '))
  49. {
  50. strRes.append(1, '+');
  51. }
  52. else
  53. {
  54. strRes.append(1, (unsigned char)str.c_str()[iIndex]);
  55. }
  56. ++iIndex;
  57. }
  58. else
  59. {
  60. char chBuf[10];
  61. sprintf_s(chBuf, "%%%02X%%%02X%%%02X", (unsigned char)str.c_str()[iIndex], (unsigned char)str.c_str()[iIndex + 1], (unsigned char)str.c_str()[iIndex + 3]);
  62. std::string strTmp = chBuf;
  63. strRes += strTmp;
  64. iIndex += 3;
  65. }
  66. }
  67. return strRes;
  68. }
  69. /* Hex string to char */
  70. char HexStrToChar(const std::string& str)
  71. {
  72. char chRet = 0;
  73. for (char i = 0; i < 2; i++)
  74. {
  75. char chTemp = str.at(i);
  76. chRet *= 16;
  77. if(chTemp >= '0' && chTemp <= '9')
  78. chRet += chTemp - '0';
  79. else if(chTemp >= 'a' && chTemp <= 'f')
  80. chRet += (chTemp - 'a') + 10;
  81. else if(chTemp >= 'A' && chTemp <= 'F')
  82. chRet += (chTemp - 'A') + 10;
  83. }
  84. return(chRet);
  85. }
  86. /* string to int */
  87. unsigned int StrToInt(const std::string& str)
  88. {
  89. unsigned int uiRet = 0;
  90. for(unsigned char ucIndex = 0; ucIndex < str.length(); ucIndex++)
  91. {
  92. char chTemp = str.at(ucIndex);
  93. uiRet *= 10;
  94. uiRet += chTemp - '0';
  95. }
  96. return(uiRet);
  97. }
  98. /************************************************************************/
  99. /* 函数:[4/2/2019 Wang];
  100. /* 描述:;
  101. /* 参数:;
  102. /* [IN] :;
  103. /* [OUT] :;
  104. /* [IN/OUT] :;
  105. /* 返回:void;
  106. /************************************************************************/
  107. unsigned char TwoHexCharToChar(char ch1,char ch2)
  108. {
  109. char Numb1;
  110. char Numb2;
  111. if (ch1 >= 'A')
  112. Numb1 = (toupper(ch1)-'0'-7)*16;
  113. else
  114. Numb1 = (ch1 - '0')*16;
  115. if (ch2 >= 'A')
  116. Numb2 = (toupper(ch2) - '0' - 7);
  117. else
  118. Numb2 = (ch2 - '0');
  119. return (Numb1 + Numb2);
  120. }
  121. /************************************************************************/
  122. /* 函数:[4/2/2019 Wang];
  123. /* 描述:16进制字符串转字节,16进制字符以空格间隔,如"AA BB CC 0A";
  124. /* 参数:;
  125. /* [IN] :;
  126. /* [OUT] :;
  127. /* [IN/OUT] :;
  128. /* 返回:void;
  129. /************************************************************************/
  130. std::string HexStr2Bytes(std::string strHex)
  131. {
  132. byte value = 0;
  133. std::string strBytes;
  134. int nSize = strHex.size();
  135. for (int i = 0; i < nSize; i+=3 )
  136. {
  137. strBytes.push_back(TwoHexCharToChar(strHex[i], strHex[i+1]));
  138. }
  139. return strBytes;
  140. }
  141. std::string Bytes2HexStr(const unsigned char *pbuffer, int nLen )
  142. {
  143. std::string hex;
  144. char szhex[5] = {0};
  145. for ( int i = 0; i < nLen; i++ )
  146. {
  147. memset(szhex, 0, 5);
  148. sprintf_s(szhex, "%02X ", pbuffer[i]);
  149. hex.append(szhex);
  150. }
  151. return hex;
  152. }
  153. //BYTE HexStrToChar(const string& szData)
  154. //{
  155. // BYTE byteRet = 0;
  156. //
  157. // for (int i = 0; i < 2; ++i)
  158. // {
  159. // BYTE byteTmp = szData.at(i);
  160. //
  161. // byteRet *= 16;
  162. //
  163. // if (byteTmp >= '0' && byteTmp <= '9')
  164. // byteRet += byteTmp - '0';
  165. // else if (byteTmp >= 'a' && byteTmp <= 'f')
  166. // byteRet += (byteTmp - 'a') + 10;
  167. // else if (byteTmp >= 'A' && byteTmp <= 'F')
  168. // byteRet += (byteTmp - 'A') + 10;
  169. // }
  170. //
  171. // return(byteRet);
  172. //}
  173. //std::string CString2string(CString csStrData)
  174. //{
  175. // int iLen = csStrData.GetLength() + 1;
  176. // char* pSrc = new char[iLen];
  177. // if (pSrc == NULL)
  178. // {
  179. // return "";
  180. // }
  181. //
  182. // memset(pSrc, 0, iLen);
  183. //
  184. // wchar_t* pwSrc = NULL;
  185. // pwSrc = (wchar_t*)(csStrData.GetBuffer(iLen * sizeof(wchar_t)));
  186. // WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pwSrc, -1, pSrc, iLen, NULL, NULL);
  187. //
  188. // string strRet = string(pSrc);
  189. // delete[] pSrc;
  190. // csStrData.ReleaseBuffer(iLen);
  191. // return strRet;
  192. //}