《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

相关文章

电脑找不到mfc90u.dll文件怎么办? 系统报错mfc90u.dll丢失修复的5种方案

《电脑找不到mfc90u.dll文件怎么办?系统报错mfc90u.dll丢失修复的5种方案》在我们日常使用电脑的过程中,可能会遇到一些软件或系统错误,其中之一就是mfc90u.dll丢失,那么,mf... 在大部分情况下出现我们运行或安装软件,游戏出现提示丢失某些DLL文件或OCX文件的原因可能是原始安装包

电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案

《电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案》最近有不少兄弟反映,电脑突然弹出“mfc100u.dll已加载,但找不到入口点”的错误提示,导致一些程序无法正... 在计算机使用过程中,我们经常会遇到一些错误提示,其中最常见的就是“找不到指定的模块”或“缺少某个DL

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Redis 热 key 和大 key 问题小结

《Redis热key和大key问题小结》:本文主要介绍Redis热key和大key问题小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、什么是 Redis 热 key?热 key(Hot Key)定义: 热 key 常见表现:热 key 的风险:二、

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制