zoukankan      html  css  js  c++  java
  • 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;
    
    
  • 相关阅读:
    jwt
    mybatis的回顾
    swagger
    MySQl总结
    Java异常
    常用Dos命令
    C++初级项目——机房预约系统
    C++中将数字型字符串转变为int类型的方法
    C++中int *a; int &a; int & *a; int * &a
    #define_CRT_SECURE_NO_WARNINGS的用法
  • 原文地址:https://www.cnblogs.com/ajanuw/p/13585798.html
Copyright © 2011-2022 走看看