诸暨麻将添加redis
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

132 行
2.9 KiB

  1. #include "stdafx.h"
  2. #include "ChineseCode.h"
  3. #include <vector>
  4. CChineseCode::CChineseCode()
  5. {
  6. }
  7. CChineseCode::~CChineseCode()
  8. {
  9. }
  10. void CChineseCode::UTF_8ToUnicode(wchar_t* pOut, char *pText){
  11. char* uchar = (char *)pOut;
  12. uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
  13. uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
  14. return;
  15. }
  16. void CChineseCode::UnicodeToUTF_8(char* pOut, wchar_t* pText){
  17. // 注意 WCHAR高低字的顺序,低字节在前,高字节在后
  18. char* pchar = (char *)pText;
  19. pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
  20. pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
  21. pOut[2] = (0x80 | (pchar[0] & 0x3F));
  22. return;
  23. }
  24. std::string CChineseCode::S_UnicodeToUTF_8( wchar_t* pText){
  25. // 注意 WCHAR高低字的顺序,低字节在前,高字节在后
  26. char* pOut = new char[4];
  27. char* pchar = (char *)pText;
  28. pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
  29. pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
  30. pOut[2] = (0x80 | (pchar[0] & 0x3F));
  31. pOut[3] = '\0';
  32. std::string strRes(pOut);
  33. delete []pOut;
  34. pOut = nullptr;
  35. return strRes;
  36. }
  37. void CChineseCode::UnicodeToGB2312(char* pOut, wchar_t uData){
  38. WideCharToMultiByte(CP_ACP, NULL, &uData, 1, pOut, sizeof(wchar_t), NULL, NULL);
  39. return;
  40. }
  41. void CChineseCode::Gb2312ToUnicode(wchar_t* pOut, char *gbBuffer){
  42. ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, gbBuffer, 2, pOut, 1);
  43. return;
  44. }
  45. CStringW CChineseCode::Utf8ToUnicode(const char* buf)
  46. {
  47. int len = ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
  48. if (len == 0) return L"";
  49. std::vector<wchar_t> unicode(len);
  50. ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);
  51. return &unicode[0];
  52. }
  53. void CChineseCode::GB2312ToUTF_8(std::string& pOut, char *pText, int pLen){
  54. char buf[4];
  55. int nLength = pLen * 3;
  56. char* rst = new char[nLength];
  57. memset(buf, 0, 4);
  58. memset(rst, 0, nLength);
  59. int i = 0;
  60. int j = 0;
  61. while (i < pLen){
  62. //如果是英文直接复制就能
  63. if (*(pText + i) >= 0){
  64. rst[j++] = pText[i++];
  65. }
  66. else{
  67. wchar_t pbuffer;
  68. Gb2312ToUnicode(&pbuffer, pText + i);
  69. UnicodeToUTF_8(buf, &pbuffer);
  70. unsigned short int tmp = 0;
  71. tmp = rst[j] = buf[0];
  72. tmp = rst[j + 1] = buf[1];
  73. tmp = rst[j + 2] = buf[2];
  74. j += 3;
  75. i += 2;
  76. }
  77. }
  78. rst[j] = 0x00;
  79. //返回结果
  80. pOut = rst;
  81. delete[]rst;
  82. return;
  83. }
  84. void CChineseCode::UTF_8ToGB2312(std::string &pOut, char *pText, int pLen){
  85. char * newBuf = new char[pLen];
  86. char Ctemp[4];
  87. memset(Ctemp, 0, 4);
  88. int i = 0;
  89. int j = 0;
  90. while (i < pLen){
  91. if (pText > 0){
  92. newBuf[j++] = pText[i++];
  93. }
  94. else{
  95. WCHAR Wtemp;
  96. UTF_8ToUnicode(&Wtemp, pText + i);
  97. UnicodeToGB2312(Ctemp, Wtemp);
  98. newBuf[j] = Ctemp[0];
  99. newBuf[j + 1] = Ctemp[1];
  100. i += 3;
  101. j += 2;
  102. }
  103. }
  104. newBuf[j] = 0x00;
  105. pOut = newBuf;
  106. delete[]newBuf;
  107. return;
  108. }