[英]Unicode project for CString to const byte*?
有人可以幫助我將CString轉換為const字節指針。 我嘗試下面的代碼,但是它不起作用。 我的程序使用Unicode設置。
Cstring hello = "MyApp";
const BYTE* pData = (const BYTE*)(LPCTSTR)hello;
謝謝。
將其解釋為ascii字符串:
CStringA asciiString( hello ); const BYTE* lpData = (const BYTE*)(LPCSTR)asciiString;
或轉換為代表本地代碼頁中字符串的字節:
CT2CA buf( hello ); const BYTE* lpData = (const BYTE*)buf;
對於初學者,您需要了解是否正在使用unicode。 默認情況下,Visual Studio喜歡制作應用程序,以便它們使用Unicode。 如果要使用ANSI(每個字母僅使用1個字節),則需要將其從Unicode轉換為ANSI。 這將為您提供BYTE *對象。 這是一種方法:
CString hello;
hello=L"MyApp"; // Unicode string
int iChars = WideCharToMultiByte( CP_UTF8,0,(LPCWSTR) hello,-1,NULL,0,NULL,NULL); // First we need to get the number of characters in the Unicode string
if (iChars == 0)
return 0; // There are no characters here.
BYTE* lpBuff = new BYTE[iChars]; // alocate the buffer with the number of characters found
WideCharToMultiByte(CP_UTF8,0,(LPCWSTR) hello,-1,(LPSTR) lpBuff,iChars-1, NULL, NULL); // And convert the Unicode to ANSI, then put the result in our buffer.
//如果要使其具有恆定字節指針,只需添加以下行:
const BYTE* cbOut = lpBuff;
現在,如果您想要的只是訪問CString本身,那么只需將其轉換為:
const TCHAR* MyString = (LPCTSTR) hello;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.