gpt4 book ai didi

c++ - 转换 unicode 字符串,反之亦然

转载 作者:IT老高 更新时间:2023-10-28 22:33:25 25 4
gpt4 key购买 nike

我对使用 Unicode 字符串和指针有点陌生,我不知道如何将 unicode 转换为 ascii,反之亦然。以下是我正在尝试做的事情,

const wchar_t *p = L"This is a string";

如果我想将其转换为 char*,如何将 wchar_t* 转换为 char*,反之亦然?

或按值使用 wstringstring 类对象,反之亦然

std::wstring wstr = L"This is a string";

如果我是正确的,你可以直接将字符串复制到一个新的缓冲区而不进行转换吗?

最佳答案

在未来(VS 2010 已经支持它),这将在标准 C++ 中成为可能(终于!):

#include <string>
#include <locale>

std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
const std::wstring wide_string = L"This is a string";
const std::string utf8_string = converter.to_bytes(wide_string);

关于c++ - 转换 unicode 字符串,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4786292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com