zoukankan      html  css  js  c++  java
  • string与wstring转换

    • 方法一:MultiByteToWideChar、WideCharToMultiByte
     1 BOOL StringToWString(const std::string &str,std::wstring &wstr)
     2  {    
     3      int nLen = (int)str.length();    
     4      wstr.resize(nLen,L' ');
     5  
     6      int nResult = MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen);
     7  
     8      if (nResult == 0)
     9      {
    10          return FALSE;
    11      }
    12  
    13      return TRUE;
    14  }
    15  //wstring高字节不为0,返回FALSE
    16  BOOL WStringToString(const std::wstring &wstr,std::string &str)
    17  {    
    18      int nLen = (int)wstr.length();    
    19      str.resize(nLen,' ');
    20  
    21      int nResult = WideCharToMultiByte(CP_ACP,0,(LPCWSTR)wstr.c_str(),nLen,(LPSTR)str.c_str(),nLen,NULL,NULL);
    22  
    23      if (nResult == 0)
    24      {
    25          return FALSE;
    26      }
    27  
    28      return TRUE;
    29  }
    • 方法二:std::copy
     1 std::wstring StringToWString(const std::string &str)
     2  {
     3      std::wstring wstr(str.length(),L' ');
     4      std::copy(str.begin(), str.end(), wstr.begin());
     5      return wstr; 
     6  }
     7  
     8  //只拷贝低字节至string中
     9  std::string WStringToString(const std::wstring &wstr)
    10  {
    11      std::string str(wstr.length(), ' ');
    12      std::copy(wstr.begin(), wstr.end(), str.begin());
    13      return str; 
    14  }
  • 相关阅读:
    Qt图片显示
    C# 对话框使用整理
    C# AutoResetEvent 使用整理
    C++ 模板
    superset使用
    superset部署
    kafka修改topic副本数
    c# 生成自定义图片
    c# 解决Randoms伪随机重复问题
    c# 图片加水印
  • 原文地址:https://www.cnblogs.com/02xiaoma/p/2597576.html
Copyright © 2011-2022 走看看