《C++笔记》 Part14 MFC的String to CString 转换在多语言系统下乱码问题

2023-12-20 07:58

本文主要是介绍《C++笔记》 Part14 MFC的String to CString 转换在多语言系统下乱码问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • @[TOC](文章目录)
    • Summary
    • C++ convert from string to LPCWSTR
    • C++ convert from LPCWSTR to string
    • 解决方法二
    • 解决方法三
    • Reference

Summary

Encountered problem in convert from string to CString (LPCWSTR), and the reverse convert, find out the way to convert between these two types and tested in Visual Studio with successful result. The unicode setting is configured in the Visual Studio project property page –> Configuration Properties –> General –> Character Set –> Use Unicode Character Set, as below picture shows,
字符集设置为Unicod


Different string types description as below, as How to convert std::string to LPCSTR? mentioned,

  • LPSTR - (long) pointer to string - char *
  • LPCSTR - (long)pointer to constant string - const char *
  • LPWSTR - (long) pointer to Unicode (wide) string - wchar_t *
  • LPCWSTR - (long) pointer to constant Unicode (wide) string - const wchar_t*
  • LPTSTR - (long) pointer to TCHAR (Unicode if UNICODE is defined, ANSI if not) string - TCHAR *
  • LPCTSTR - (long) pointer to constant TCHAR string - const TCHAR *

C++ convert from string to LPCWSTR

As you know, std::string is char* type, while LPCWSTR ,LPWSTR or CString is wchar_t* as long as the Visual Studio configured as Unicode Character Set.

I am using How to convert std::string to LPCSTR? solution as below code solved this problem,

LPWSTR ConvertString(const std::string& instr)
{// Assumes std::string is encoded in the current Windows ANSI codepageint bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);if (bufferlen == 0){// Something went wrong. Perhaps, check GetLastError() and log.return 0;}// Allocate new LPWSTR - must deallocate it laterLPWSTR widestr = new WCHAR[bufferlen + 1];::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);// Ensure wide string is null terminatedwidestr[bufferlen] = 0;// Do something with widestrreturn widestr;//delete[] widestr;
}---------------------

Refer to the How to convert string to LPCTSTR? solution 5, it is the similar solution as above by using MultiByteToWideChar function,

//该转换方式可解决英文及其它语言系统中文字符乱码问题—解决方法一
USES_CONVERSION;
std::wstring s2ws(const std::string& s)
{int len;int slength = (int)s.length() + 1;len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);wchar_t* buf = new wchar_t[len];MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);std::wstring r(buf);delete[] buf;return r;
}std::string s;#ifdef UNICODE
std::wstring stemp = s2ws(s); // Temporary buffer is required
LPCWSTR result = stemp.c_str();
#else
LPCWSTR result = s.c_str();
#endif>---------------------

C++ convert from LPCWSTR to string

To convert from LPCWSTR to string, can split into two steps, first step convert from CString to wchar_t, 2nd step, convert from wchar_t to char*, as below code shows,

 //convert from CString to char* , first from CString to wchar_t then to char *wchar_t wCharString = sFile.GetBuffer(sFile.GetLength()+1); //CString to wchar_tsize_t origsize = wcslen(wCharString) + 1;size_t convertedChars = 0;char gszFile[100] = {0};wcstombs_s(&convertedChars, gszFile, origsize, wCharString , _TRUNCATE); //from wchar_t to char*

Below code from MSDN is working perfectly for the conversion from wchar_t* to char* by using wcstombs_s function,


// crt_wcstombs_s.c
// This example converts a wide character
// string to a multibyte character string.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>#define BUFFER_SIZE 100int main( void )
{size_t   i;char      *pMBBuffer = (char *)malloc( BUFFER_SIZE );wchar_t*pWCBuffer = L"Hello, world.";printf( "Convert wide-character string:\n" );// Conversionwcstombs_s(&i, pMBBuffer, (size_t)BUFFER_SIZE, pWCBuffer, (size_t)BUFFER_SIZE );// Outputprintf("   Characters converted: %u\n", i);printf("    Multibyte character: %s\n\n",pMBBuffer );// Free multibyte character bufferif (pMBBuffer){free(pMBBuffer);}
}

解决方法二

CString string2CString(string str)
{USES_CONVERSION;return A2T((LPSTR)(str.c_str()));
}

解决方法三

CString string2CString(string str)
{USES_CONVERSION;CString cstr;cstr.Format(_T("%s"),str.c_str());return cstr;
}

Reference

  1. MSDN, Converts a sequence of wide characters to a corresponding sequence of multibyte characters
  2. How to convert string to LPCTSTR?
  3. How to convert std::string to LPCSTR?

这篇关于《C++笔记》 Part14 MFC的String to CString 转换在多语言系统下乱码问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/515283

相关文章

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操