CAFE

MFC와 OpenCV

Unicode와 Multibyte 변환 CString char*

작성자한창호|작성시간16.08.11|조회수792 목록 댓글 0

다음 소스를 참고하면 변환이 용이하다. 인터넷에 있는 몇가지 소스이다. 참고 하세요.

 

1.  첫번째 소스

#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
 
int main()
{
        ifstream in_file("UTF_file.txt", ios::in);
        in_file.seekg(3L, ios_base::beg);
       
        if(!in_file.is_open())
        {
               cout << "ERROR!! Cann't Open File" << endl;
               in_file.close();
               return 0;
        }
 
        char buf[1024];
        memset(buf, 0, 1024); 
        in_file.read(buf, 1024);
        in_file.close();
 
        // Get nLength of the Wide Char buffer
        int nLength = MultiByteToWideChar(CP_UTF8, 0, buf, 1024, NULL, NULL);
        wchar_t *UTF_buf = new wchar_t[nLength]; 
        // Change UTF-8 to Unicode (UTF-16)
        MultiByteToWideChar(CP_UTF8, 0, buf, lstrlen(buf) + 1, UTF_buf, nLength);
 
        // Get nLength of the multi byte buffer
        nLength = WideCharToMultiByte(CP_ACP, 0, UTF_buf, -1, NULL, 0, NULL, NULL);       
        char *Ansi_buf = new char[nLength]; 
        // Change from unicode to mult byte
        WideCharToMultiByte(CP_ACP, 0, UTF_buf, -1, Ansi_buf, nLength, NULL, NULL);
 
        delete [] UTF_buf; 
        cout << Ansi_buf << endl; 
        ofstream out_file("ANSI_file.txt", ios_base::trunc);
        out_file.write(Ansi_buf, strlen(Ansi_buf));
        out_file.close();
 
        return 0;
}

 

2. 두번째 소스

 

 1) 유니코드 -> 멀티바이트

char* ConvertUnicodeToMultybyte(CString strUnicode)
{
 int nLen = WideCharToMultiByte(CP_ACP, 0, strUnicode, -1, NULL, 0, NULL, NULL);


 char* pMultibyte  = new char[nLen];
 memset(pMultibyte, 0x00, (nLen)*sizeof(char));

 WideCharToMultiByte(CP_ACP, 0, strUnicode, -1, pMultibyte, nLen, NULL, NULL);

 return pMultibyte;
}

2) 멀티바이트 -> 유니코드

CString ConvertMultibyteToUnicode(char* pMultibyte)
{
 int nLen = strlen(pMultibyte);


 WCHAR *pWideChar = new WCHAR[nLen];
 memset(pWideChar, 0x00, (nLen)*sizeof(WCHAR));

 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pMultibyte, -1, pWideChar, nLen);

 CString strUnicode;
 strUnicode.Format(_T("%s"), pWideChar);

 delete [] pWideChar;

 return strUnicode;
}

 

사용예시

 

CString strUnicode(_T("유니코드"));
char* pMultibyte = ConvertUnicodeToMultibyte(strUnicode);delete [] pMultibyte;

 

char* pMultibyte = "멀티바이트";
CString strUnicode = ConvertMultibyteToUnicode(pMultibyte);

 

 

3. string 사용

wstring strUni = CA2W("멀티바이트를 유니코드로 변환");
string strMulti = CW2A(L"유니코드를 멀티바이트로 변환");
string strUTF8 = CW2A(L"유니코드를 UTF8로변환",CP_UTF8);

소켓을 통해 문자를 전송시 멀티바이트로 전송한다.

std::string msg = CW2A(message);
m_pDataSocket->Send(msg.c_str(), msg .length()); 

 

4. CString 변환

 #include "atlstr.h"


CString char2CString(char* inp)
{
 int len;
 CString str;
 BSTR buf;
 len = MultiByteToWideChar(CP_ACP, 0, inp, (int)strlen(inp), NULL, NULL);
 buf = SysAllocStringLen(NULL, len);
 MultiByteToWideChar(CP_ACP, 0, inp, (int)strlen(inp), buf, len);
 str.Format(_T("%s"), buf);
 return str;
}

char* CString2char(CString& str)
{
 long len = str.GetLength();
 len = len*2;
 char* szTemp = new char[len+1];
 memset(szTemp, 0, len+1);
 USES_CONVERSION;
 strcpy(szTemp, T2A(str));
 return szTemp;
}

 

Unicode 환경에서 CString --> char* 형변환

CString  str;              // CString 변수
wchar_t* wchar_str;  // wild character 변수
char*  char_str;        // char* 형의 변수
int  char_str_len;       // char* 형 변수의 길이
 
// CString to wchar_t* 의 형변환
wchar_str = str.GetBuffer(str.GetLength());



// char형의 길이를 구하여 배열 메모리 할당 
char_str_len = WideCharToMultiByte(CP_ACP, 0, wchar_str, -1, NULL, 0, NULL, NULL);
char_str = new char[char_str_len];  //메모리 할당



// wchar_t* to char* 의 형변환

WideCharToMultiByte(CP_ACP, 0, wchar_str, -1, char_str, char_str_len, 0,0);  

 

기타 변환 코드

변환명코드
ansi -> utf8#include <WTypes.h >

//ANSI to UTF8
string ansi_to_utf8(string& ansi)
{
    WCHAR unicode[1024];
    char utf8[1024];
    memset(unicode, 0, sizeof(unicode));
    memset(utf8, 0, sizeof(utf8));
    ::MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, unicode, sizeof(unicode));
    ::WideCharToMultiByte(CP_UTF8, 0, unicode, -1, utf8, sizeof(utf8), NULL, NULL);
    return string(utf8);
}
utf8 -> ansi//UTF8 to ANSI
string utf8_to_ansi(string& utf8)
{
    WCHAR unicode[1024];
    char ansi[1024];
    memset(unicode, 0, sizeof(unicode));
    memset(ansi, 0, sizeof(ansi));
    ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, unicode, sizeof(unicode));
    ::WideCharToMultiByte(CP_ACP, 0, unicode, -1, ansi, sizeof(ansi), NULL, NULL);
    return string(ansi);
}
WideByte -> MultiByte// WideByte -> MultiByte
string WBCS(const wchar_t strUni[256]) {
    char strUtf8[256] = { 0, };
    int nLen = WideCharToMultiByte(CP_UTF8, 0, strUni, lstrlenW(strUni), NULL, 0, NULL, NULL);
    WideCharToMultiByte(CP_UTF8, 0, strUni, lstrlenW(strUni), strUtf8, nLen, NULL, NULL);
    return strUtf8;
}
MultiByte -> WideByte// MultiByte -> WideByte
wchar_t* MBCS(const char* strMultibyte) {
    wchar_t strUnicode[256] = { 0, };
    int nLen = MultiByteToWideChar(CP_ACP, 0, strMultibyte, strlen(strMultibyte), NULL, NULL);
    MultiByteToWideChar(CP_ACP, 0, strMultibyte, strlen(strMultibyte), strUnicode, nLen);
    return strUnicode;
}

 

 

출처:

 http://cafe.naver.com/cppmaster.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=1788&

http://egloos.zum.com/sukhwalj/v/6033774

 

 

다음검색
현재 게시글 추가 기능 열기
  • 북마크
  • 신고 센터로 신고

댓글

댓글 리스트
맨위로

카페 검색

카페 검색어 입력폼