2012-12-15 6 views
0

나는 인코딩을 알고 있으며 입력 문자열은 100 % 싱글 바이트이고 utf 등의 멋진 인코딩은 없다. 그리고 원하는 것은 wchar_t * 또는 wstring으로 알려진 인코딩을 기반으로 변환하는 것이다. 어떤 기능을 사용합니까? btowc() 다음 루프? 어쩌면 문자열 객체가 뭔가 유용 할 수 있습니다. 많은 예제가 있지만 모두 "멀티 바이트"또는 btowc()를 사용하여 화면에 출력을 표시하는 방법을 보여주는 환상적인 루프입니다. 실제로이 함수가 작동하는 것으로 나타났습니다. 그런 버퍼를 다루는 방법을 본 적이 없습니다. 상황은 항상 넓은 char 2 배 단일 char 문자열보다 큰가요?C++에서 단일 바이트 문자열을 넓은 문자열로 변환하는 방법은 무엇입니까?

+1

입니다 ? 표준 C++을 고수하고 싶습니까? –

+0

나는 표준 C++을 고집하는 편이 좋지만, winapi가 그것을하는 쉬운 방법이라면 - 나에게는 문제가되지 않는다. 둘째, 인코딩은'windows-1250'이지만 ISO 8859-5 키릴 문자와 같은 다른 것일 수 있습니다. – rsk82

+1

여기에있는 예를 참조하십시오. http://en.cppreference.com/w/cpp/string/multibyte/mbstowcs –

답변

2

시도해보십시오. template 그것은 나에게 아주 잘 복무했다.

(저자의 알 수없는이)

글쎄, 무슨 인코딩
/* string2wstring.h */ 
#pragma once 

#include <string> 
#include <vector> 
#include <locale> 
#include <functional> 
#include <iostream> 

// Put this class in your personal toolbox... 
template<class E, 
class T = std::char_traits<E>, 
class A = std::allocator<E> > 

class Widen : public std::unary_function< 
    const std::string&, std::basic_string<E, T, A> > 
{ 
    std::locale loc_; 
    const std::ctype<E>* pCType_; 

    // No copy-constructor, no assignment operator... 
    Widen(const Widen&); 
    Widen& operator= (const Widen&); 

public: 
    // Constructor... 
    Widen(const std::locale& loc = std::locale()) : loc_(loc) 
    { 
#if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6.0... 
     using namespace std; 
     pCType_ = &_USE(loc, ctype<E>); 
#else 
     pCType_ = &std::use_facet<std::ctype<E> >(loc); 
#endif 
    } 

    // Conversion... 
    std::basic_string<E, T, A> operator() (const std::string& str) const 
    { 
     typename std::basic_string<E, T, A>::size_type srcLen = 
      str.length(); 
     const char* pSrcBeg = str.c_str(); 
     std::vector<E> tmp(srcLen); 

     pCType_->widen(pSrcBeg, pSrcBeg + srcLen, &tmp[0]); 
     return std::basic_string<E, T, A>(&tmp[0], srcLen); 
    } 
}; 

// How to use it... 
int main() 
{ 
Widen<wchar_t> to_wstring; 
std::string s = "my test string"; 
std::wstring w = to_wstring(s); 
std::wcout << w << L"\n"; 
} 
+0

일부 오프 사이트 페이지에 대한 링크를 게시하지 마십시오. 여기에 콘텐츠를 제공하십시오. –

+0

내가 편집했지만 링크도 제공 할 수 있습니까? 저자를 알지 못하고 코드의 출처를 보여주고 싶기 때문입니다. – marscode

+1

가능한 경우 링크를 제공하고 적절한 속성을 부여하십시오. –

관련 문제