상세 컨텐츠

본문 제목

std::string과 std::wstring간의 문자열 변환 관련

본문

반응형

 

 

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

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

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

 

 

 

출처: http://myblue0324.tistory.com/118

 

 

1. std::string을 std::wstring으로 변환.

  1. // 방법1.  
  2. std::string str = "string";  
  3. std::wstring wstr = L"";  
  4.   
  5. wstr.assign(str.begin(), str.end());  
  6.   
  7. // 방법2.   
  8. USES_CONVERSION;  
  9.   
  10. std::string str = "string";  
  11. std::wstring wstr(A2W(str.c_str()));  


2. std::wstring을 std::string으로 변환.

  1. // 방법1.  
  2. std::wstring wstr = L"string";  
  3. std::string str = "";  
  4.   
  5. str.assign(wstr.begin(), wstr.end());  
  6.   
  7. // 방법2.  
  8. USES_UTF8_CONVERSION;  
  9.   
  10. std::wstring wstr = L"string";  
  11. str::string str(W2A(wstr.c_str()));  


위의 문자열을 CString형으로 변환하고자 할 경우에는 간단하게 아래와 같이 변경이 가능합니다.

  1. #ifdef _UNICODE  
  2.     std::wstring s = L"string";  
  3.     CString str(s.c_str());   
  4. #else  
  5.     std::string s = "string";  
  6.     CString str(s.c_str());   
  7. #endif  



출처: http://myblue0324.tistory.com/118 [나만의 상상력]

 

 

 

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

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

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

 

 

 

출처: https://stackoverflow.com/questions/4804298/how-to-convert-wstring-into-string

 

 

As Cubbi pointed out in one of the comments, std::wstring_convert (C++11) provides a neat simple solution:

wstring string_to_convert;  //setup converter using convert_type = std::codecvt_utf8<wchar_t>; std::wstring_convert<convert_type, wchar_t> converter;  //use converter (.to_bytes: wstr->str, .from_bytes: str->wstr) std::string converted_str = converter.to_bytes( string_to_convert );

I was using a combination of wcstombs and tedious allocation/deallocation of memory before I came across this.

http://en.cppreference.com/w/cpp/locale/wstring_convert

update(2013.11.28)

One liners can be stated as so (Thank you Guss for your comment):

std::wstring str = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes("some string");

Wrapper functions can be stated as so: (Thank you ArmanSchwarz for your comment)

wstring s2ws(const std::string& str) {     using convert_typeX = std::codecvt_utf8<wchar_t>;     std::wstring_convert<convert_typeX, wchar_t> converterX;      return converterX.from_bytes(str); }  string ws2s(const std::wstring& wstr) {     using convert_typeX = std::codecvt_utf8<wchar_t>;     std::wstring_convert<convert_typeX, wchar_t> converterX;      return converterX.to_bytes(wstr); }

Note: there's some controversy on whether string/wstring should be passed in to functions as references or as literals (due to C++11 and compiler updates). I'll leave the decision to the person implementing, but it's worth knowing.

Note: I'm using std::codecvt_utf8 in the above code, but if you're not using UTF-8 you'll need to change that to the appropriate encoding you're using:

http://en.cppreference.com/w/cpp/header/codecvt

 

 

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

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

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

 

 

 

출처: http://wendys.tistory.com/40

 

 

convert string to wstring

유니코드 환경과 멀티바이 환경이 함께 동작하는 윈도우 환경에선 사용하는 경우에 따라 멀티바이트 스트링을 사용하기도 하고, 유니코드 스트링을 사용하기도 합니다.

이 때, std::string 을 이용하여 아주 간단하게 상호 변환이 가능합니다.

 

그냥 넣으면 안되나요??

네 안됩니다... 아래와 같이 에러가 납니다

 

 

 

 

그래서 아래와같은 정상적인 방법을 이용하여 컨버팅을 할 수 있습니다.

 

멀티바이트 -> 유니코드

 

{
    std::string message_a = "convert before message";
    std::wstring message_w;
 
    message_w.assign(message_a.begin(), message_a.end());
    wprintf(message_w.c_str());
}

 

 

 

유니코드 -> 멀티바이트

 
{
    std::wstring message_w = L"convert before message";
    std::string message_a;
     
    message_a.assign(message_w.begin(), message_w.end());
    printf(message_a.c_str());
}


출처: http://wendys.tistory.com/40 [Hacker N0te]
 

아주 간단하죠?

 

assign 이란 대입이라는 의미이며 std::string message = "assign message"와 동일한 기능입니다.



출처: http://wendys.tistory.com/40 [Hacker N0te]

 

 

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

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

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

 

 

반응형


관련글 더보기

댓글 영역