구글


문자열 변경 ▶SW_Coding

/ *

 * AnsiToUnicode converts the ANSI string pszA to a Unicode string

 * and returns the Unicode string through ppszW. Space for the

 * the converted string is allocated by AnsiToUnicode.

 */

 

HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW)

{

 

    ULONG cCharacters;

    DWORD dwError;

 

    // If input is null then just return the same.

    if (NULL == pszA)

    {

        *ppszW = NULL;

        return NOERROR;

    }

 

    // Determine number of wide characters to be allocated for the

    // Unicode string.

    cCharacters =  strlen(pszA)+1;

 

    // Use of the OLE allocator is required if the resultant Unicode

    // string will be passed to another COM component and if that

    // component will free it. Otherwise you can use your own allocator.

    *ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);

    if (NULL == *ppszW)

        return E_OUTOFMEMORY;

 

    // Covert to Unicode.

    if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,

                  *ppszW, cCharacters))

    {

        dwError = GetLastError();

        CoTaskMemFree(*ppszW);

        *ppszW = NULL;

        return HRESULT_FROM_WIN32(dwError);

    }

 

    return NOERROR;

}

/*

 * UnicodeToAnsi converts the Unicode string pszW to an ANSI string

 * and returns the ANSI string through ppszA. Space for the

 * the converted string is allocated by UnicodeToAnsi.

 */

 

HRESULT __fastcall UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA)

{

 

    ULONG cbAnsi, cCharacters;

    DWORD dwError;

 

    // If input is null then just return the same.

    if (pszW == NULL)

    {

        *ppszA = NULL;

        return NOERROR;

    }

 

    cCharacters = wcslen(pszW)+1;

    // Determine number of bytes to be allocated for ANSI string. An

    // ANSI string can have at most 2 bytes per character (for Double

    // Byte Character Strings.)

    cbAnsi = cCharacters*2;

 

    // Use of the OLE allocator is not required because the resultant

    // ANSI  string will never be passed to another COM component. You

    // can use your own allocator.

    *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);

    if (NULL == *ppszA)

        return E_OUTOFMEMORY;

 

    // Convert to ANSI.

    if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA,

                  cbAnsi, NULL, NULL))

    {

        dwError = GetLastError();

        CoTaskMemFree(*ppszA);

        *ppszA = NULL;

        return HRESULT_FROM_WIN32(dwError);

    }

    return NOERROR;

 

}

 


TCHAR --> CString

CStirng sSring;
TCHAR sTString = _T("가나다");

sString.Format(_T(%s), sTString);


CString --> TCHAR

TCHAR * des = new TCHAR[sString.GetLength() + 1]; // 1은 NULL을 위한 여유공간
_tcscpy(des, NowSaving.GetBuffer(0));

================================================================================

String - > UTF - 8

char buffer[20];

WCHAR Unicode[20];

char UTF8code[20];
int nUnicodeSize = MultiByteToWideChar(CP_ACP, 0, lpID, strlen(lpID), Unicode, sizeof(Unicode));

int nUTF8codeSize =
    WideCharToMultiByte(CP_UTF8, 0, Unicode, nUnicodeSize, UTF8code, sizeofUTF8code), NULL, NULL);

nUnicodeSize = MultiByteToWideChar(CP_UTF8, 0, UTF8code, nUTF8codeSize, Unicode, sizeof(Unicode));

memcpy(buffer, UTF8code, nUTF8codeSize);

이렇게 하면 buffer에 UTF-8로 변환

UTF-8 - > Unicode 

void CCPimFONEDevice::UTF82UNICODE(char * strSrc,wchar_t *strTarget)
{
  int nSize = MultiByteToWideChar(CP_UTF8, 0, strSrc ,  -1 , 0 , 0);
  MultiByteToWideChar(CP_UTF8, 0, strSrc ,  -1 , strTarget, nSize);
}


BSTR은 SysFreeString 로 반드시 메모리 해제......
=============================================================================================
BSTR -> THCHAR
void  CCPimFONEDevice::BSTR2TCHAR(TCHAR * ptchar, BSTR bstr)
{
 CComVariant vtIn;
 USES_CONVERSION;
 strcpy(ptchar, OLE2A(bstr));
}

TCHAR -> BSTR
void  CCPimFONEDevice::TCHAR2BSTR(TCHAR * ptchar, BSTR *bstr)
{
 CComVariant vtIn;
 USES_CONVERSION;
 vtIn = ptchar;
 *bstr = vtIn.bstrVal;


}

===============================================================================================

wchar --> BSTR
wchar m_pszCallID [50];
BSTR bstr;
bstr= SysAllocString(m_pszCallID)


if (bstr!= NULL)
 SysFreeString(bstr)

BSTR -->wchar
wcscpy(pszCallID , bstr);

if (bstr!= NULL)
 SysFreeString(bstr)
================================================================================================
CString --> BSTR

 CString sTemp;
 BSTR bstrStr = sTemp.AllocSysString();
if (bstrStr != NULL)
 SysFreeString(bstrStr )


BSTR --> CString
 #include <atlbase.h>

CString sTemp, sTemp2;
BSTR bstr;
sTemp= "가나다라..";
bstr = sTemp.AllocSysString();
CComBSTR bstTemp;
bstTemp = bstr;
sTemp2 = bstTemp.Copy();
bstTemp.Detach();
if (bstr != NULL)
 SysFreeString(bstr)

Or

char* lpszText;
char lpszName[ 256 ];
lpszText = _com_util::ConvertBSTRToString( bstr);
strcpy( lpszName, lpszText );
delete []lpszText;
sTemp2.format("%s", lpszName);
if (bstr != NULL)
 SysFreeString(bstr)

 TIP==========================================
CString strError((LPCSTR)IDS_ERROR_FAILED);
문자열 리소스의 아이디는 반드시 (LPCSTR)로 캐스팅해야 합니다.
 


덧글

댓글 입력 영역