• c++ string and wstring conversion


    wchar_t to char

    #include <comdef.h>
    
    const wchar_t* exepath = L"d:\中文 路径\中文 路径.exe";
    _bstr_t b(exepath);
    const char* c = b;
    printf("%s
    ", c);
    

    MultiByteToWideChar string to wstring

      setlocale(LC_ALL, "chs");
      string s1 = "中文 こんにちは";
    
      size_t wstr_size = MultiByteToWideChar(CP_ACP, 0, s1.c_str(), s1.length(), NULL, NULL);
      if (wstr_size == NULL) return 0;
    
      wstring s2;
      s2.resize(wstr_size);
    
      MultiByteToWideChar(CP_ACP, 0, s1.c_str(), s1.length(), (LPWSTR)s2.data(), s2.length());
    
      printf("%s
    ", s1.c_str());
      printf("%ls
    ", s2.c_str());
    

    WideCharToMultiByte wstring to string

      setlocale(LC_ALL, "chs");
      wstring s1 = L"中文 こんにちは";
    
      size_t str_size = WideCharToMultiByte(CP_ACP, 0, s1.data(), s1.length(), NULL, NULL, 0, 0);
      if (str_size == NULL) return 0;
    
      string s2;
      s2.resize(str_size);
    
      WideCharToMultiByte(CP_ACP, 0, s1.data(), s1.length(), (LPSTR)s2.data(), s2.length(), 0, 0);
    
      printf("%ls
    ", s1.c_str());
      printf("%s
    ", s2.c_str());
      return 0;
    
    
  • 相关阅读:
    决定迁移过来,深耕于此。。。
    一篇搞定MongoDB
    一篇搞定vue请求和跨域
    自定义全局组件
    一篇搞定vue-router
    一篇搞定Vuex
    vue系列
    .Vue.js大全
    一篇搞定spring Jpa操作数据库
    自定义admin
  • 原文地址:https://www.cnblogs.com/ajanuw/p/13585798.html
Copyright © 2020-2023  润新知