zoukankan      html  css  js  c++  java
  • 多字节与UTF-8、Unicode之间的转换

    from http://blog.csdn.net/frankiewang008/article/details/12832239

    1. // 多字节编码转为UTF8编码  
    2. bool MBToUTF8(vector<char>& pu8, const char* pmb, int32 mLen)  
    3. {  
    4.  // convert an MBCS string to widechar   
    5.  int32 nLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);  
    6.    
    7.  WCHAR* lpszW = NULL;  
    8.  try  
    9.  {  
    10.     lpszW = new WCHAR[nLen];  
    11.  }  
    12.  catch(bad_alloc &memExp)  
    13.  {  
    14.     return false;  
    15.  }  
    16.   
    17.  int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, lpszW, nLen);  
    18.    
    19.  if(nRtn != nLen)  
    20.  {  
    21.     delete[] lpszW;  
    22.     return false;  
    23.  }  
    24.  // convert an widechar string to utf8  
    25.  int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, NULL, 0, NULL, NULL);  
    26.  if (utf8Len <= 0)  
    27.  {  
    28.      return false;  
    29.  }  
    30.  pu8.resize(utf8Len);  
    31.  nRtn = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, &*pu8.begin(), utf8Len, NULL, NULL);  
    32.  delete[] lpszW;  
    33.   
    34.  if (nRtn != utf8Len)  
    35.  {  
    36.      pu8.clear();  
    37.      return false;  
    38.  }  
    39.  return true;  
    40. }  
    41.   
    42. // UTF8编码转为多字节编码  
    43. bool UTF8ToMB(vector<char>& pmb, const char* pu8, int32 utf8Len)  
    44. {  
    45.     // convert an UTF8 string to widechar   
    46.     int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0);  
    47.   
    48.     WCHAR* lpszW = NULL;  
    49.     try  
    50.     {  
    51.         lpszW = new WCHAR[nLen];  
    52.     }  
    53.     catch(bad_alloc &memExp)  
    54.     {  
    55.         return false;  
    56.     }  
    57.   
    58.     int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, lpszW, nLen);  
    59.   
    60.     if(nRtn != nLen)  
    61.     {  
    62.         delete[] lpszW;  
    63.         return false;  
    64.     }  
    65.   
    66.     // convert an widechar string to Multibyte   
    67.     int32 MBLen = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, NULL, 0, NULL, NULL);  
    68.     if (MBLen <=0)  
    69.     {  
    70.         return false;  
    71.     }  
    72.     pmb.resize(MBLen);  
    73.     nRtn = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, &*pmb.begin(), MBLen, NULL, NULL);  
    74.     delete[] lpszW;  
    75.   
    76.     if(nRtn != MBLen)  
    77.     {  
    78.         pmb.clear();  
    79.         return false;  
    80.     }  
    81.     return true;  
    82. }  
    83.   
    84. // 多字节编码转为Unicode编码  
    85. bool MBToUnicode(vector<wchar_t>& pun, const char* pmb, int32 mLen)  
    86. {  
    87.     // convert an MBCS string to widechar   
    88.     int32 uLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);  
    89.   
    90.     if (uLen<=0)  
    91.     {  
    92.         return false;  
    93.     }  
    94.     pun.resize(uLen);  
    95.   
    96.     int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, &*pun.begin(), uLen);  
    97.   
    98.     if (nRtn != uLen)  
    99.     {  
    100.         pun.clear();  
    101.         return false;  
    102.     }  
    103.     return true;  
    104. }  
    105.   
    106. //Unicode编码转为多字节编码  
    107. bool UnicodeToMB(vector<char>& pmb, const wchar_t* pun, int32 uLen)  
    108. {  
    109.     // convert an widechar string to Multibyte   
    110.     int32 MBLen = WideCharToMultiByte(CP_ACP, 0, pun, uLen, NULL, 0, NULL, NULL);  
    111.     if (MBLen <=0)  
    112.     {  
    113.         return false;  
    114.     }  
    115.     pmb.resize(MBLen);  
    116.     int nRtn = WideCharToMultiByte(CP_ACP, 0, pun, uLen, &*pmb.begin(), MBLen, NULL, NULL);  
    117.   
    118.     if(nRtn != MBLen)  
    119.     {  
    120.         pmb.clear();  
    121.         return false;  
    122.     }  
    123.     return true;  
    124. }  
    125.   
    126. // UTF8编码转为Unicode  
    127. bool UTF8ToUnicode(vector<wchar_t>& pun, const char* pu8, int32 utf8Len)  
    128. {  
    129.     // convert an UTF8 string to widechar   
    130.     int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0);  
    131.     if (nLen <=0)  
    132.     {  
    133.         return false;  
    134.     }  
    135.     pun.resize(nLen);  
    136.     int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, &*pun.begin(), nLen);  
    137.   
    138.     if(nRtn != nLen)  
    139.     {  
    140.         pun.clear();  
    141.         return false;  
    142.     }  
    143.   
    144.     return true;  
    145. }  
    146.   
    147. // Unicode编码转为UTF8  
    148. bool UnicodeToUTF8(vector<char>& pu8, const wchar_t* pun, int32 uLen)  
    149. {  
    150.     // convert an widechar string to utf8  
    151.     int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, NULL, 0, NULL, NULL);  
    152.     if (utf8Len<=0)  
    153.     {  
    154.         return false;  
    155.     }  
    156.     pu8.resize(utf8Len);  
    157.     int32 nRtn = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, &*pu8.begin(), utf8Len, NULL, NULL);  
    158.   
    159.     if (nRtn != utf8Len)  
    160.     {  
    161.         pu8.clear();  
    162.         return false;  
    163.     }  
    164.     return true;  
    165. }  
  • 相关阅读:
    HDU3336 Count the string —— KMP next数组
    CodeForces
    51Nod 1627 瞬间移动 —— 组合数学
    51Nod 1158 全是1的最大子矩阵 —— 预处理 + 暴力枚举 or 单调栈
    51Nod 1225 余数之和 —— 分区枚举
    51Nod 1084 矩阵取数问题 V2 —— 最小费用最大流 or 多线程DP
    51Nod 机器人走方格 V3 —— 卡特兰数、Lucas定理
    51Nod XOR key —— 区间最大异或值 可持久化字典树
    HDU4825 Xor Sum —— Trie树
    51Nod 1515 明辨是非 —— 并查集 + 启发式合并
  • 原文地址:https://www.cnblogs.com/kex1n/p/4138321.html
Copyright © 2011-2022 走看看