• Windows下ANSI与UTF8之间转换,wstring与string之间的转换代码片段


    Windows下ANSI与UTF8之间转换,wstring与string之间的转换代码片段

    参考:multiByteToWideChar 函数 (stringapiset.h) - Win32 apps | Microsoft Learn

    static std::string UTF82ANSI(const char* utf8)
    {
    	std::string str;
    #if _WIN32
    	int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    	if (len <= 0)
    		return str;
    	wchar_t* wstr = new wchar_t[len + 1];
    	memset(wstr, 0, len + 1);
    	MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    	len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    	char* buf = new char[len + 1];
    	memset(buf, 0, len + 1);
    	WideCharToMultiByte(CP_ACP, 0, wstr, -1, buf, len, NULL, NULL);
    
    	str.assign(buf, len - 1);
    	delete[] wstr;
    	delete[] buf;
    #else
    	str = utf8;
    #endif
    	return std::move(str);
    }
    
    static std::string ANSI2UTF8(const char* gb2312)
    {
    	std::string str;
    #if _WIN32
    	int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
    	if (len <= 0)
    		return str;
    	wchar_t* wstr = new wchar_t[len + 1];
    	memset(wstr, 0, len + 1);
    	MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
    	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
    	char* buf = new char[len + 1];
    	memset(buf, 0, len + 1);
    	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, buf, len, NULL, NULL);
    	
    	str.assign(buf, len - 1);
    	delete[] wstr;
    	delete[] buf;
    #else
    	str = gb2312;
    #endif
    	return std::move(str);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    wstring与string直接转换

    windows版

    static std::wstring to_wide_string(std::string& strascii)
    {
    #ifdef _MSC_VER
    	int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);	
    	if (widesize == ERROR_NO_UNICODE_TRANSLATION)
    	{
    		throw std::exception("Invalid UTF-8 sequence.");
    	}
    	if (widesize == 0)
    	{
    		throw std::exception("Error in conversion.");
    	}
    	std::vector resultstring(widesize);
    	int convresult = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);
    	if (convresult != widesize)
    	{
    		throw std::exception("La falla!");
    	}
    
    	return std::wstring(&resultstring[0]);
    #endif
    }
    
    static std::string to_byte_string(const std::wstring& widestring)
    { 
    #ifdef _MSC_VER
    	int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
    	if (utf8size == 0)
    	{
    		throw std::exception("Error in conversion.");
    	}
    	std::vector resultstring(utf8size);
    
    	int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);
    
    	if (convresult != utf8size)
    	{
    		throw std::exception("La falla!");
    	}
    
    	return std::string(&resultstring[0]);
    #endif
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    C++11版

    #include 
    #include 
    #include 
    
    // convert string to wstring
    inline std::wstring to_wide_string(const std::string& input)
    {
    	std::wstring_convert> converter;
    	return converter.from_bytes(input);
    }
    // convert wstring to string 
    inline std::string to_byte_string(const std::wstring& input)
    {
    	std::wstring_convert> converter;
    	return converter.to_bytes(input);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    DAY35:序列化与反序列化
    taichi库记录
    elementui的el-dialog组件与el-tabs同时用导致浏览器卡死的原因解决
    26 | 信任始于握手:TLS1.2连接过程解析
    IP-guard flexpaper远程命令执行漏洞复现 [附POC]
    计算机毕业设计php_thinkphp_vue的学生公寓管理系统-宿舍管理-寝室管理(源码+系统+mysql数据库+Lw文档)
    多模态知识问答:MMCoQA: Conversational Question Answering over Text, Tables, and Images
    【Java】链表的中间结点
    12 正太总体参数的假设检验
    PMP备考大全:经典题库(8月第4周)
  • 原文地址:https://blog.csdn.net/youzai2017/article/details/128118361