c++ ANSI、UNICODE、UTF8互转

简介: static std::wstring MBytesToWString(const char* lpcszString);    static std::string WStringToMBytes(const wchar_t* lpwcszWString);    static std::...
    static std::wstring MBytesToWString(const char* lpcszString);
    static std::string WStringToMBytes(const wchar_t* lpwcszWString);
    static std::wstring UTF8ToWString(const char* lpcszString);
    static std::string WStringToUTF8(const wchar_t* lpwcszWString);

 

std::wstring KKLogObject::MBytesToWString(const char* lpcszString)
{
    int len = strlen(lpcszString);
    int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, lpcszString, -1, NULL, 0);
    wchar_t* pUnicode = new wchar_t[unicodeLen + 1];
    memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
    ::MultiByteToWideChar(CP_ACP, 0, lpcszString, -1, (LPWSTR)pUnicode, unicodeLen);
    wstring wString = (wchar_t*)pUnicode;
    delete [] pUnicode;
    return wString;
}

std::string KKLogObject::WStringToMBytes(const wchar_t* lpwcszWString)
{
    char* pElementText;
    int iTextLen;
    // wide char to multi char
    iTextLen = ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, -1, NULL, 0, NULL, NULL);
    pElementText = new char[iTextLen + 1];
    memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
    ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, 0, pElementText, iTextLen, NULL, NULL);
    std::string strReturn(pElementText);
    delete [] pElementText;
    return strReturn;
}

std::wstring KKLogObject::UTF8ToWString(const char* lpcszString)
{
    int len = strlen(lpcszString);
    int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, lpcszString, -1, NULL, 0);
    wchar_t* pUnicode;
    pUnicode = new wchar_t[unicodeLen + 1];
    memset((void*)pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
    ::MultiByteToWideChar(CP_UTF8, 0, lpcszString, -1, (LPWSTR)pUnicode, unicodeLen);
    wstring wstrReturn(pUnicode);
    delete [] pUnicode;
    return wstrReturn;
}

std::string KKLogObject::WStringToUTF8(const wchar_t* lpwcszWString)
{
    char* pElementText;
    int iTextLen = ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString, -1, NULL, 0, NULL, NULL);
    pElementText = new char[iTextLen + 1];
    memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
    ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString, -1, pElementText, iTextLen, NULL, NULL);
    std::string strReturn(pElementText);
    delete [] pElementText;
    return strReturn;
}
目录
相关文章
|
6月前
|
C++
HTML+JavaScript构建一个将C/C++定义的ANSI字符串转换为MASM32定义的DWUniCode字符串的工具
HTML+JavaScript构建一个将C/C++定义的ANSI字符串转换为MASM32定义的DWUniCode字符串的工具
|
编译器 C++
C++ 使用HttpRequest获取网页及UTF8转GB码
C++ 使用HttpRequest获取网页及UTF8转GB码
163 0
C++常用类型转换(在Unicode和MBCS之间相互转换函数)
C++常用类型转换(在Unicode和MBCS之间相互转换函数)
268 0
|
C++
Visual C++ unicode and utf8 转换
ATL宏: USES_CONVERSION; W2A A2W   CString StringUtil::UTF8_to_UNICODE(const char *utf8_string, int length){    int wcsLen = ::MultiByteToWideChar(...
935 0
|
API Windows
VC++中多字节字符集和Unicode之间的互换
在Visual C++.NET中,默认的字符集是Unicode,这和Windows默认的字符集是一致的,不过在老的VC6.0等工程中,默认的字符集形式是多字节字符集(MBCS:Multi-Byte Character Set),这样导致在VC6.0中非常简单实用的各类字符操作和函数在VS2005环境下运行时会报各种各样的错误,这里总结了在Visual C++.NET2005环境中Unicode字符集下CString和char *之间相互转换的几种方法,其实也就是Unicode字符集与MBCS字符集转换。
1338 0
|
API C++ Windows
VC++的Unicode编程
一、什么是Unicode  先从ASCII说起,ASCII是用来表示英文字符的一种编码规范。每个ASCII字符占用1个字节,因此,ASCII编码可以表示的最大字符数是255(00H—FFH)。
1056 0
|
12天前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。