std :: string을 LPCSTR로 변환하는 방법?
를 어떻게로 변환 할 std::string
수 LPCSTR
있습니까? 또한을 어떻게로 변환 할 std::string
수 LPWSTR
있습니까?
나는 이것들 LPCSTR
LPSTR
LPWSTR
과 완전히 혼동된다 LPCWSTR
.
인가 LPWSTR
와 LPCWSTR
같은?
str.c_str()
는 (상수 STRing에 const char *
대한 LPCSTR
긴 포인터) 0
인을 제공합니다. 이는 종료 된 문자열에 대한 포인터임을 의미합니다 . W
넓은 문자열을 의미합니다 ( wchar_t
대신 대신 구성됨 char
).
c_str()
에서 const char *
( LPCSTR
) 을 받으려면 호출 하십시오 std::string
.
그것은 모두 이름에 있습니다.
LPSTR
-문자열에 대한 (긴) 포인터- char *
LPCSTR
-상수 문자열에 대한 (긴) 포인터- const char *
LPWSTR
-유니 코드 (와이드) 문자열에 대한 (긴) 포인터- wchar_t *
LPCWSTR
-상수 유니 코드 (와이드) 문자열에 대한 (긴) 포인터- const wchar_t *
LPTSTR
-TCHAR (유니 코드가 정의 된 경우 유니 코드, 그렇지 않은 경우 ANSI)에 대한 (긴) 포인터 문자열- TCHAR *
LPCTSTR
-상수 TCHAR 문자열에 대한 (긴) 포인터- const TCHAR *
이름의 L (긴) 부분은 무시해도됩니다. 16 비트 Windows와는 다릅니다.
다음에 해당하는 Microsoft 정의 typedef입니다.
LPCSTR : null로 끝나는 const 문자열의 포인터 char
LPSTR : 널 종료 문자 스트링의 포인터 char
(종종 버퍼가 전달되어 '출력'매개 변수로 사용됨)
LPCWSTR : null로 끝나는 const 문자열에 대한 포인터 wchar_t
LPWSTR : 널 종료 문자열의 포인터 wchar_t
(종종 버퍼가 전달되어 '출력'매개 변수로 사용됨)
a std::string
를 LPCSTR 로 "변환"하는 것은 정확한 컨텍스트에 달려 있지만 일반적으로 호출 .c_str()
하면 충분합니다.
작동합니다.
void TakesString(LPCSTR param);
void f(const std::string& param)
{
TakesString(param.c_str());
}
이런 식으로 시도해서는 안됩니다.
LPCSTR GetString()
{
std::string tmp("temporary");
return tmp.c_str();
}
에 의해 반환 된 버퍼 .c_str()
는 std::string
인스턴스 가 소유 하며 문자열이 다음에 수정되거나 파괴 될 때까지만 유효합니다.
a std::string
를 a 로 변환하는 LPWSTR
것이 더 복잡합니다. 을 원하는 것은 LPWSTR
당신이 수정 버퍼를 필요로하고 당신은 또한 당신이 이해하는지 확인해야한다는 것을 의미 인코딩 문자std::string
사용하고 있습니다. std::string
시스템 기본 인코딩을 사용하여 문자열을 포함하는 경우 (여기서 가정) 필요한 와이드 문자 버퍼의 길이를 찾고 MultiByteToWideChar
(Win32 API 함수)를 사용하여 트랜스 코딩을 수행 할 수 있습니다.
예 :
void f(const std:string& instr)
{
// Assumes std::string is encoded in the current Windows ANSI codepage
int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
if (bufferlen == 0)
{
// Something went wrong. Perhaps, check GetLastError() and log.
return;
}
// Allocate new LPWSTR - must deallocate it later
LPWSTR widestr = new WCHAR[bufferlen + 1];
::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
// Ensure wide string is null terminated
widestr[bufferlen] = 0;
// Do something with widestr
delete[] widestr;
}
사용 LPWSTR
하면 문자열의 내용을 변경할 수 있습니다. 사용 LPCWSTR
하면 문자열의 내용을 변경할 수 없습니다.
std::string s = SOME_STRING;
// get temporary LPSTR (not really safe)
LPSTR pst = &s[0];
// get temporary LPCSTR (pretty safe)
LPCSTR pcstr = s.c_str();
// convert to std::wstring
std::wstring ws;
ws.assign( s.begin(), s.end() );
// get temporary LPWSTR (not really safe)
LPWSTR pwst = &ws[0];
// get temporary LPCWSTR (pretty safe)
LPCWSTR pcwstr = ws.c_str();
LPWSTR
is just a pointer to original string. You shouldn't return it from function using the sample above. To get not temporary LPWSTR
you should made a copy of original string on the heap. Check the sample below:
LPWSTR ConvertToLPWSTR( const std::string& s )
{
LPWSTR ws = new wchar_t[s.size()+1]; // +1 for zero at the end
copy( s.begin(), s.end(), ws );
ws[s.size()] = 0; // zero at the end
return ws;
}
void f()
{
std::string s = SOME_STRING;
LPWSTR ws = ConvertToLPWSTR( s );
// some actions
delete[] ws; // caller responsible for deletion
}
The MultiByteToWideChar
answer that Charles Bailey gave is the correct one. Because LPCWSTR
is just a typedef for const WCHAR*
, widestr
in the example code there can be used wherever a LPWSTR
is expected or where a LPCWSTR
is expected.
One minor tweak would be to use std::vector<WCHAR>
instead of a manually managed array:
// using vector, buffer is deallocated when function ends
std::vector<WCHAR> widestr(bufferlen + 1);
::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), &widestr[0], bufferlen);
// Ensure wide string is null terminated
widestr[bufferlen] = 0;
// no need to delete; handled by vector
Also, if you need to work with wide strings to start with, you can use std::wstring
instead of std::string
. If you want to work with the Windows TCHAR
type, you can use std::basic_string<TCHAR>
. Converting from std::wstring
to LPCWSTR
or from std::basic_string<TCHAR>
to LPCTSTR
is just a matter of calling c_str
. It's when you're changing between ANSI and UTF-16 characters that MultiByteToWideChar
(and its inverse WideCharToMultiByte
) comes into the picture.
Converting is simple:
std::string myString;
LPCSTR lpMyString = myString.c_str();
One thing to be careful of here is that c_str does not return a copy of myString, but just a pointer to the character string that std::string wraps. If you want/need a copy you'll need to make one yourself using strcpy.
The conversion is simple:
std::string str; LPCSTR lpcstr = str.c_str();
The easiest way to convert a std::string
to a LPWSTR
is in my opinion:
- Convert the
std::string
to astd::vector<wchar_t>
- Take the address of the first
wchar_t
in the vector.
std::vector<wchar_t>
has a templated ctor which will take two iterators, such as the std::string.begin()
and .end()
iterators. This will convert each char to a wchar_t
, though. That's only valid if the std::string
contains ASCII or Latin-1, due to the way Unicode values resemble Latin-1 values. If it contains CP1252 or characters from any other encoding, it's more complicated. You'll then need to convert the characters.
std::string myString("SomeValue");
LPSTR lpSTR = const_cast<char*>(myString.c_str());
myString is the input string and lpSTR is it's LPSTR equivalent.
참고URL : https://stackoverflow.com/questions/1200188/how-to-convert-stdstring-to-lpcstr
'program story' 카테고리의 다른 글
git-로컬이 삭제되었지만 파일이 원격에 존재할 때 병합 충돌 (0) | 2020.08.04 |
---|---|
드롭 다운 목록에 빈 항목이없는 빈 HTML SELECT (0) | 2020.08.03 |
사용자가 소프트 키보드를 닫았을 때 감지 (0) | 2020.08.03 |
값의 배열에 루비 해시 (0) | 2020.08.03 |
스위프트 4에서 열거 형 Decodable을 어떻게 만듭니 까? (0) | 2020.08.03 |