#pragma once #include #include #include #include "squirrel.h" #include "sqstdaux.h" #include "sqstdblob.h" #include "sqstdio.h" #include "sqstdmath.h" #include "sqstdstring.h" #include "sqstdsystem.h" class TOOL { public: TOOL() {}; ~TOOL() {}; private: public: static char* U8ToU16(const char* szU8) { //预转换,得到所需空间的大小 int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0); //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间 wchar_t* wszString = new wchar_t[wcsLen + 1]; //转换 ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen); //最后加上'\0' wszString[wcsLen] = '\0'; char* m_char; int len = WideCharToMultiByte(CP_ACP, 0, wszString, wcslen(wszString), NULL, 0, NULL, NULL); m_char = new char[len + 1]; WideCharToMultiByte(CP_ACP, 0, wszString, wcslen(wszString), m_char, len, NULL, NULL); delete[]wszString; m_char[len] = '\0'; return m_char; } static std::string SquirrelU2W(const SQChar* Str) { char* wbuffer = (char*)(Str); size_t len = 0; while (wbuffer[len] != 0 || wbuffer[len - 1] != 0) { ++len; } char* cbuffer = new char[len / 2 + 1]; int k = 0; for (size_t i = 0; i < len; i += 2) { cbuffer[k] = wbuffer[i]; ++k; } cbuffer[k] = '\0'; char* Text = U8ToU16(cbuffer); delete[]cbuffer; std::string RetStr(Text); delete[]Text; return RetStr; } static char* ConvertAnsiToUtf8(const char* szAnsi) { // 第一步:将ANSI字符串转换为宽字符字符串 int wcsLen = ::MultiByteToWideChar(CP_ACP, 0, szAnsi, -1, NULL, 0); wchar_t* wszString = new wchar_t[wcsLen]; ::MultiByteToWideChar(CP_ACP, 0, szAnsi, -1, wszString, wcsLen); // 第二步:将宽字符字符串转换回UTF-8字符串 int utf8Len = ::WideCharToMultiByte(CP_UTF8, 0, wszString, wcsLen, NULL, 0, NULL, NULL); char* szUtf8 = new char[utf8Len]; ::WideCharToMultiByte(CP_UTF8, 0, wszString, wcsLen, szUtf8, utf8Len, NULL, NULL); delete[] wszString; // 释放宽字符字符串的内存 szUtf8[utf8Len - 1] = '\0'; // 确保字符串以'\0'结尾 return szUtf8; } static std::wstring charTowchar_t(std::string Str) { char* Sn = ConvertAnsiToUtf8(Str.c_str()); std::string B(Sn); std::wstring Ret(B.begin(), B.end()); delete[]Sn; //std::wcout << Ret << std::endl; return Ret; } };