将CString转换为const byte*的Unicode项目
Unicode project for CString to const byte*?
有人可以帮助我转换CString到const字节指针。我尝试下面的代码,但它不工作。我的程序使用Unicode设置。
Cstring hello = "MyApp";
const BYTE* pData = (const BYTE*)(LPCTSTR)hello;
谢谢。
从如何将CString转换为BYTE指针:
-
将其解释为ascii字符串:
CStringA asciiString( hello ); const BYTE* lpData = (const BYTE*)(LPCSTR)asciiString;
-
或转换为表示本地代码页中的字符串的字节:
CT2CA buf( hello ); const BYTE* lpData = (const BYTE*)buf;
Try (PCWSTR)
文档位于http://msdn.microsoft.com/en-us/library/8a994dfk(v=vs.80).aspx
对于初学者,您需要了解是否使用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;
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 既然存在危险,为什么项目要使用-I include开关
- cmake在我的项目中所需的所有静态库都不成功
- QT通过C++添加映射QML项目
- C++ - Unicode Newline
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 如何在 unicode 项目中将 std:string 转换为 CString
- 如何在Visual Studio中处理Unicode和非Unicode项目的混合
- 创建进程在转换项目以支持 Unicode 后不运行 .cmd 文件
- 将CString转换为const byte*的Unicode项目
- 如何将 ANSI 项目转换为 UNICODE 项目
- 将字符串流传递给unicode项目中的控制台输出
- 在MBCS项目中使用unicode dll,反之亦然