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]