C++ 字符串相互转换 适合 lua project
#include <iostream> #include <Windows.h> #include <assert.h> #define Main main void wchar2char(char *Buf,const wchar_t* wchar) { int len = WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), NULL, 0, NULL, NULL); WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), Buf, len, NULL, NULL); } //———————————————— //版权声明:本文为CSDN博主「bailang_zhizun」的原创文章,遵循CC 4.0 by - sa版权协议,转载请附上原文出处链接及本声明。 //原文链接:https ://blog.csdn.net/bailang_zhizun/article/details/80348282 wchar_t * char2wchar(const char* cchar) { wchar_t *m_wchar; int len = MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), NULL, 0); m_wchar = new wchar_t[len + 1]; MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), m_wchar, len); m_wchar[len] = '\0'; return m_wchar; } void UTF82WCS(char *buf,const char* szU8) { //预转换,得到所需空间的大小; int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0); //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间 wchar_t wszString[1024] = { 0 }; //转换 ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen); //最后加上'\0' wszString[wcsLen] = '\0'; wchar2char(buf, wszString); } //———————————————— //版权声明:本文为CSDN博主「游学四方」的原创文章,遵循CC 4.0 by - sa版权协议,转载请附上原文出处链接及本声明。 //原文链接:https ://blog.csdn.net/blackbattery/article/details/78244178 char* UnicodeToUtf8(const wchar_t* unicode) { int len; len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL); char *szUtf8 = new char[90]; memset(szUtf8, 0, len + 1); WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL); return szUtf8; } int Main(int Argc,char **Argv) { if (Argc == 1) { assert(false); } char buf[1024] = { 0 }; char Buf[1024] = { 0 }; for (int i = 0; i < Argc - 1; i++) { UTF82WCS(Buf, Argv[i + 1]); strcat_s(buf, Buf); strcat_s(buf, "\r\n"); } printf(buf); system("pause"); return 0; }