本文介绍了将C ++ UTF-8/ASCII转换为MFC中的UTF-16的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将其显示在MFC程序中之前,如何将(文本)文件从UTF-8/ASCII转换为UTF-16?因为MFC每个字符使用16位,而Windows上的大多数(文本)文件使用UTF-8或ASCII.

How can I convert a (text) file from UTF-8/ASCII to UTF-16 before it will be displaying in a MFC program?Because MFC uses 16 bits per character and the most (text) files on windows use UTF-8 or ASCII.

推荐答案

简单的答案称为 MultiByteToWideChar WideCharToMultiByte 进行反向转换.还有 CW2A CA2W 使用起来更简单.

The simple answer is called MultiByteToWideChar and WideCharToMultiByte to do the reverse conversion. There's also CW2A and CA2W that are a little simpler to use.

但是,我强烈建议不要直接使用这些功能.您有手动处理字符缓冲区的痛苦,并且有创建内存损坏或安全漏洞的风险.

However, I would strongly recommand against using these functions directly. You have the pain of handling character buffers manually with the risk of creating memory corruption or security holes.

使用基于std :: string和/或迭代器的库要好得多.例如, utf8cpp .这具有体积小,仅标头和多平台的优点.

It's much better to use a library based on std::string and/or iterators. For example, utf8cpp. This one has the advantage to be small, header-only and multiplatform.

这篇关于将C ++ UTF-8/ASCII转换为MFC中的UTF-16的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 17:03