`unicode to ansi` C++ Examples

16 C++ code examples are found related to "unicode to ansi". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: Utils.cpp    From TinyAntivirus with GNU General Public License v2.0 7 votes vote down vote up
StringA UnicodeToAnsi(__in StringW * str)
{
	if (str == NULL) return "";

	int nRequired = WideCharToMultiByte(CP_UTF8, 0, str->c_str(), (int)str->length(), NULL, 0, NULL, NULL);
	if (nRequired == 0) return "";

	CHAR * lpWideChar = new CHAR[nRequired + 1];
	if (lpWideChar == NULL) return "";
	nRequired = WideCharToMultiByte(CP_UTF8, 0, str->c_str(), (int)str->length(), lpWideChar, nRequired, NULL, NULL);
	if (nRequired == 0) return "";
	lpWideChar[nRequired] = '\0';
	StringA wstr = lpWideChar;
	delete[]lpWideChar;
	return wstr;
} 
Example 2
Source File: StringTools.cpp    From duilib with MIT License 6 votes vote down vote up
std::wstring AnsiToUnicode(const std::string& strSource)
{
    int nLength = ::MultiByteToWideChar(CP_ACP, 0, strSource.data(), -1, NULL, 0);

    if (nLength < 1)
    {
        return L"";
    }

    std::wstring strResult(nLength, 0);
    ::MultiByteToWideChar(CP_ACP, 0, strSource.data(), strSource.size(), &strResult[0], nLength);

    return std::wstring(strResult.data(), nLength - 1);
} 
Example 3
Source File: StringTools.cpp    From duilib with MIT License 6 votes vote down vote up
std::string UnicodeToAnsi(const std::wstring& strSource)
{
    int nLength = ::WideCharToMultiByte(CP_ACP, 0, strSource.data(), -1, NULL, 0, NULL, FALSE);

    if (nLength < 1)
    {
        return "";
    }

    std::vector<char> vecResult(nLength);
    ::WideCharToMultiByte(CP_ACP, 0, strSource.data(), -1, &vecResult[0], nLength, NULL, FALSE);

    return std::string(vecResult.begin(), vecResult.end()-1);
} 
Example 4
Source File: Utils.cpp    From Telegram-Anti-Revoke with MIT License 6 votes vote down vote up
string			UnicodeToAnsi(const wstring &String)
	{
		string Result;
		INT Length = WideCharToMultiByte(CP_ACP, 0, String.c_str(), (INT)String.length(), NULL, 0, NULL, NULL);
		Result.resize(Length);
		WideCharToMultiByte(CP_ACP, 0, String.c_str(), (INT)String.length(), (CHAR*)Result.data(), Length, NULL, NULL);
		return Result;
	} 
Example 5
Source File: EncodingUtil.cpp    From TinyIM with MIT License 6 votes vote down vote up
wchar_t* EncodeUtil::AnsiToUnicode(const char* lpszStr)
{
    wchar_t* lpUnicode;
    int nLen;

    if (NULL == lpszStr)
        return NULL;

    nLen = ::MultiByteToWideChar(CP_ACP, 0, lpszStr, -1, NULL, 0);
    if (0 == nLen)
        return NULL;

    lpUnicode = new wchar_t[nLen + 1];
    if (NULL == lpUnicode)
        return NULL;

    memset(lpUnicode, 0, sizeof(wchar_t)* (nLen + 1));
    nLen = ::MultiByteToWideChar(CP_ACP, 0, lpszStr, -1, lpUnicode, nLen);
    if (0 == nLen)
    {
        delete[]lpUnicode;
        return NULL;
    }

    return lpUnicode;
} 
Example 6
Source File: tiostream.cpp    From czys with GNU Lesser General Public License v3.0 5 votes vote down vote up
std::string unicodeToAnsi(const wchar_t *wstr)
  {
    if(SystemSupportsUnicode) {
      debug("unicodeToAnsi() - Should not be used on WinNT systems.");
    }

    const int len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    if(len == 0)
      return std::string();

    std::string str(len, '\0');
    WideCharToMultiByte(CP_ACP, 0, wstr, -1, &str[0], len, NULL, NULL);

    return str;
  } 
Example 7
Source File: Utils.cpp    From TinyAntivirus with GNU General Public License v2.0 5 votes vote down vote up
StringW AnsiToUnicode(__in StringA * str)
{
	if (str == NULL) return L"";

	int nRequired = MultiByteToWideChar(CP_UTF8, 0, str->c_str(), (int)str->length(), NULL, 0);
	if (nRequired == 0) return L"";

	WCHAR * lpWideChar = new WCHAR[nRequired + 1];
	if (lpWideChar == NULL) return L"";
	nRequired = MultiByteToWideChar(CP_UTF8, 0, str->c_str(), (int)str->length(), lpWideChar, nRequired);
	if (nRequired == 0) return L"";
	lpWideChar[nRequired] = L'\0';
	StringW wstr = lpWideChar;
	delete[]lpWideChar;
	return wstr;
} 
Example 8
Source File: Encode.cpp    From CKF3Alpha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
wchar_t *ANSIToUnicode(const char *str)
{
	static wchar_t result[1024];
	int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
	MultiByteToWideChar(CP_ACP, 0, str, -1, result, len);
	result[len] = '\0';
	return result;
} 
Example 9
Source File: Encode.cpp    From CKF3Alpha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
char *UnicodeToANSI(const wchar_t *str)
{
	static char result[1024];
	int len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
	WideCharToMultiByte(CP_ACP, 0, str, -1, result, len, NULL, NULL);
	result[len] = '\0';
	return result;
} 
Example 10
Source File: StringUtility.cpp    From service-fabric with MIT License 5 votes vote down vote up
void StringUtility::UnicodeToAnsi(std::wstring const &uniString, std::string &dst)
    {
        wchar_t const *str = uniString.c_str();
        DWORD size = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
        system_error_if_not(size, microsoft::GetLastErrorCode(), "Size of UnicodeToAnsi string would have been 0.");
        cbuffer buffer(size + 2, '\0');
        size = ::WideCharToMultiByte(CP_ACP, 0, str, -1, &buffer[0], size + 1, NULL, NULL);
        system_error_if_not(size, microsoft::GetLastErrorCode(), "Size of UnicodeToAnsi string would have been 0.");
        dst = &buffer[0];
    } 
Example 11
Source File: UnicodeAnsi.cpp    From FarManager with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
char* UnicodeToAnsi(const wchar_t* lpSrc, int CodePage)
{
	if ( !lpSrc )
		return NULL;

	int nLength = WideCharToMultiByte(CodePage, 0, lpSrc, -1, NULL, 0, NULL, NULL);

	char* lpResult = (char*)malloc(nLength+1);

	WideCharToMultiByte(CodePage, 0, lpSrc, -1, lpResult, nLength, NULL, NULL);

	return lpResult;
} 
Example 12
Source File: UnicodeAnsi.cpp    From FarManager with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
wchar_t* AnsiToUnicode(const char* lpSrc, int CodePage)
{
	if ( !lpSrc )
		return NULL;

	int nLength = MultiByteToWideChar(CodePage, 0, lpSrc, -1, NULL, 0);

	wchar_t* lpResult = (wchar_t*)malloc((nLength+1)*sizeof(wchar_t));

	MultiByteToWideChar(CodePage, 0, lpSrc, -1, lpResult, nLength);

	return lpResult;
} 
Example 13
Source File: EncodingUtil.cpp    From TinyIM with MIT License 5 votes vote down vote up
std::wstring EncodeUtil::AnsiToUnicode(const std::string& strAnsi)
{
    std::wstring strUnicode;

    wchar_t* lpszUnicode = EncodeUtil::AnsiToUnicode(strAnsi.c_str());
    if (lpszUnicode != NULL)
    {
        strUnicode = lpszUnicode;
        delete[]lpszUnicode;
    }

    return strUnicode;
} 
Example 14
Source File: EncodingUtil.cpp    From TinyIM with MIT License 5 votes vote down vote up
char* EncodeUtil::UnicodeToAnsi(const wchar_t* lpszStr)
{
    char* lpAnsi;
    int nLen;

    if (NULL == lpszStr)
        return NULL;

    nLen = ::WideCharToMultiByte(CP_ACP, 0, lpszStr, -1, NULL, 0, NULL, NULL);
    if (0 == nLen)
        return NULL;

    lpAnsi = new char[nLen + 1];
    if (NULL == lpAnsi)
        return NULL;

    memset(lpAnsi, 0, nLen + 1);
    nLen = ::WideCharToMultiByte(CP_ACP, 0, lpszStr, -1, lpAnsi, nLen, NULL, NULL);
    if (0 == nLen)
    {
        delete[]lpAnsi;
        return NULL;
    }

    return lpAnsi;
} 
Example 15
Source File: EncodingUtil.cpp    From TinyIM with MIT License 5 votes vote down vote up
std::string EncodeUtil::UnicodeToAnsi(const std::wstring& strUnicode)
{
    std::string strAnsi;

    char* lpszAnsi = UnicodeToAnsi(strUnicode.c_str());
    if (lpszAnsi != NULL)
    {
        strAnsi = lpszAnsi;
        delete[]lpszAnsi;
    }

    return strAnsi;
}