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;
    
    
  • 相关阅读:
    线程之Thread
    如何通过HTTP优雅调用第三方-Feign
    Spring AOP核心源码
    Spring-beans之BeanFactory初始化
    spring-core源码走读
    power of two
    排序算法(二)两种选择排序
    排序算法(一)四种冒泡排序
    约瑟夫问题
    我理解的CLH
  • 原文地址:https://www.cnblogs.com/ajanuw/p/13585798.html
Copyright © 2011-2022 走看看