源码资源网 发表于 2024-9-22 14:48:11

VC++中UNICODE与UTF-8相互转换的示例源码

VC++中UNICODE与UTF-8相互转换的示例源码
在VC++中,进行UNICODE与UTF-8之间的相互转换可以使用Windows API中的 `MultiByteToWideChar` 和 `WideCharToMultiByte` 函数。以下是一个示例代码,展示如何在Unicode( `wchar_t` 或 `CString` )和UTF-8( `char*` 或 `std::string` )之间进行转换。
### 1. UNICODE 转 UTF-8
#include <windows.h>
#include <atlstr.h> // 包含CString的定义
#include <string>
#include <iostream>
// 将UNICODE字符串(CString)转换为UTF-8字符串(std::string)
std::string UnicodeToUTF8(const CString& unicodeStr) {
    int sizeNeeded = WideCharToMultiByte(CP_UTF8, 0, unicodeStr, -1, NULL, 0, NULL, NULL);
    std::string utf8Str(sizeNeeded, 0);
    WideCharToMultiByte(CP_UTF8, 0, unicodeStr, -1, &utf8Str, sizeNeeded, NULL, NULL);
    return utf8Str;
}

### 2. UTF-8 转 UNICODE
#include <windows.h>
#include <atlstr.h> // 包含CString的定义
#include <string>
#include <iostream>
// 将UTF-8字符串(std::string)转换为UNICODE字符串(CString)
CString UTF8ToUnicode(const std::string& utf8Str) {
    int sizeNeeded = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, NULL, 0);
    CString unicodeStr;
    if (sizeNeeded > 0) {
      wchar_t* buffer = new wchar_t; // 创建缓冲区
      MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, buffer, sizeNeeded);
      buffer = 0; // 确保字符串以NULL结尾
      unicodeStr = buffer; // 将wchar_t转换为CString
      delete[] buffer; // 释放缓冲区
    }
    return unicodeStr;
}

### 3. 示例使用
int main() {
    // 示例:UNICODE转UTF-8
    CString unicodeStr = _T("Hello, 世界!"); // UNICODE字符串
    std::string utf8Str = UnicodeToUTF8(unicodeStr);
    std::cout << "UTF-8: " << utf8Str << std::endl;
   // 示例:UTF-8转UNICODE
    std::string inputUtf8Str = "Hello, 世界!";
    CString convertedUnicodeStr = UTF8ToUnicode(inputUtf8Str);
    AfxMessageBox(convertedUnicodeStr); // 显示转换后的UNICODE字符串
   return 0;
}

### 说明
1. **UnicodeToUTF8**:此函数将 `CString` (UNICODE)转换为 `std::string` (UTF-8)。
2. **UTF8ToUnicode**:此函数将 `std::string` (UTF-8)转换为 `CString` (UNICODE)。
3. **内存管理**:在 `UTF8ToUnicode` 函数中,动态分配的缓冲区需要在使用后释放,以避免内存泄漏。
4. **示例使用**:在 `main` 函数中展示了如何使用这两个转换函数。
通过以上代码,你可以在VC++中轻松地进行UNICODE与UTF-8之间的转换。


static/image/hrline/1.gif


将上面代码更改一下,不管是输入还是返回,都是CString类型

好的,下面是将输入和返回值都更改为`CString`类型的代码示例。在这个版本中,我们将`CString`用于输入和输出,而不使用`std::string` 。这样可以使代码更加一致,并且更符合 MFC 的使用习惯。
### 1. UNICODE 转 UTF-8( `CString`到`CString` )
#include <windows.h>
#include <atlstr.h> // 包含CString的定义
#include <iostream>
// 将UNICODE字符串(CString)转换为UTF-8字符串(CString)
CString UnicodeToUTF8(const CString& unicodeStr) {
    int sizeNeeded = WideCharToMultiByte(CP_UTF8, 0, unicodeStr, -1, NULL, 0, NULL, NULL);
    CString utf8Str;
    if (sizeNeeded > 0) {
      char* buffer = new char; // 创建缓冲区
      WideCharToMultiByte(CP_UTF8, 0, unicodeStr, -1, buffer, sizeNeeded, NULL, NULL);
      buffer = 0; // 确保字符串以NULL结尾
      utf8Str = buffer; // 将char*转换为CString
      delete[] buffer; // 释放缓冲区
    }
    return utf8Str;
}

### 2. UTF-8 转 UNICODE( `CString`到`CString` )
#include <windows.h>
#include <atlstr.h> // 包含CString的定义
// 将UTF-8字符串(CString)转换为UNICODE字符串(CString)
CString UTF8ToUnicode(const CString& utf8Str) {
    int sizeNeeded = MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, NULL, 0);
    CString unicodeStr;
    if (sizeNeeded > 0) {
      wchar_t* buffer = new wchar_t; // 创建缓冲区
      MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, buffer, sizeNeeded);
      buffer = 0; // 确保字符串以NULL结尾
      unicodeStr = buffer; // 将wchar_t转换为CString
      delete[] buffer; // 释放缓冲区
    }
    return unicodeStr;
}CString UTF8ToUnicode(const CStringA& utf8Str) { // 使用CStringA接收UTF-8字符串
    int sizeNeeded = MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, NULL, 0);
    CString unicodeStr;
    if (sizeNeeded > 0) {
      wchar_t* buffer = new wchar_t[sizeNeeded + 1; // 创建缓冲区
      MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, buffer, sizeNeeded);
      buffer = 0; // 确保字符串以NULL结尾
      unicodeStr = buffer; // 将wchar_t转换为CString
      delete[] buffer; // 释放缓冲区
    }
    return unicodeStr;
}

### 3. 示例使用
int main() {
    // 示例:UNICODE转UTF-8
    CString unicodeStr = _T("Hello, 世界!"); // UNICODE字符串
    CString utf8Str = UnicodeToUTF8(unicodeStr);
    std::wcout << L"UTF-8: " << (LPCTSTR)utf8Str << std::endl; // 输出UTF-8字符串
   // 示例:UTF-8转UNICODE
    CString inputUtf8Str = "Hello, 世界!"; // UTF-8编码的CString
    CString convertedUnicodeStr = UTF8ToUnicode(inputUtf8Str);
    AfxMessageBox(convertedUnicodeStr); // 显示转换后的UNICODE字符串
   return 0;
}

### 说明
1. **UnicodeToUTF8**:此函数将`CString` (UNICODE)转换为`CString` (UTF-8)。这里使用`char*`来处理 UTF-8 字符串。
2. **UTF8ToUnicode**:此函数将`CString` (UTF-8)转换为`CString` (UNICODE)。
3. **内存管理**:在转换过程中,动态分配的缓冲区需要在使用后释放,以避免内存泄漏。
4. **示例使用**:在`main`函数中展示了如何使用这两个转换函数。
通过以上代码,你可以在VC++中实现`CString`类型的 UNICODE 与 UTF-8 之间的相互转换。

页: [1]
查看完整版本: VC++中UNICODE与UTF-8相互转换的示例源码

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。