如何将string型转化为LPCWSTR型

maoyin123456789 2011-04-20 08:48:11
如题,如何将中文的string类型,转化为LPCWSTR型
比如:std::string filename(c:\\我的文档\\作业.doc);如何转化为LPCWSTR型,请高手指教
...全文
1032 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyy410874116 2011-04-22
  • 打赏
  • 举报
回复
L("变量")
qq120848369 2011-04-22
  • 打赏
  • 举报
回复
wchar_t buf1[100];
char buf2[100];
strcpy(buf2,filename.c_str());
multibytestowidechar( buf1,buf2 );

buf1应该就是了。
maoyin123456789 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 dourgulf 的回复:]

乱码不一定就是错的,UTF-8格式的汉子,如果不是支持UNICODE的编辑器打开的话,必然就是乱码。
LZ的问题有必要好好系统的学习一下关于字符编码的系统知识才能搞清楚这个问题--推荐《Windows核心编程》,或者网上找一下一个叫”无废话XML“的文章,里面有关于字符编码的很好的讲解。
或者,你简单一点的话就用wstring代替string看看行不行(wstring str = L"汉字"……
[/Quote]
是应该好好 找本编码的书来研究一下了,结贴了,感谢各位了
子达如何 2011-04-20
  • 打赏
  • 举报
回复
乱码不一定就是错的,UTF-8格式的汉子,如果不是支持UNICODE的编辑器打开的话,必然就是乱码。
LZ的问题有必要好好系统的学习一下关于字符编码的系统知识才能搞清楚这个问题--推荐《Windows核心编程》,或者网上找一下一个叫”无废话XML“的文章,里面有关于字符编码的很好的讲解。
或者,你简单一点的话就用wstring代替string看看行不行(wstring str = L"汉字";)
maoyin123456789 2011-04-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 al_ways 的回复:]
好像用L("文字")吧~你试试
[/Quote]

能给个例子吗,不太明白你的意思,L(string的变量)?
Al_Ways 2011-04-20
  • 打赏
  • 举报
回复
好像用L("文字")吧~你试试
maoyin123456789 2011-04-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 happymawolf 的回复:]
哦,那我也不知道了,其实我对这个也不熟,正好借这个机会学习下!帮你顶顶!
[/Quote]
恩,谢谢了,求达人解答。
书虫 2011-04-20
  • 打赏
  • 举报
回复
哦,那我也不知道了,其实我对这个也不熟,正好借这个机会学习下!帮你顶顶!
maoyin123456789 2011-04-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 happymawolf 的回复:]
LPCWSTR stringToLPCWSTR(std::string orig)
{
size_t origsize = orig.length() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(……
[/Quote]
这个代码我看过,好像只能转化英文的,如果有中文的话,就都变成乱码了。。。不知是不是这样
书虫 2011-04-20
  • 打赏
  • 举报
回复
LPCWSTR stringToLPCWSTR(std::string orig)
{
size_t origsize = orig.length() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(orig.length()-1));
mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);

return wcstring;
}

参考:
http://hi.baidu.com/ruisen/blog/item/601dc124a5c0cb074c088de9.html
maoyin123456789 2011-04-20
  • 打赏
  • 举报
回复
自己先顶个
wince上各种类转换特别是字符串是很头疼的,这个类是项目用的, 每个都测试好用,使用静态方法,直接引用后,写【::cv::就可以用】 typedef int u32; //转换类,所有的转换函数在这里静态 2是to的意思 //cs:cstring //s:string //i:int class cv { public: static char * s2charx(string s); //static char * cs2charx_unicode(CString s); static char * cs2charx_unicode(CString s); static CString charx2cs(char *s); cv(); virtual ~cv(); static int cstring_to_int(CString t); static CString s2cs(string text); static CString i2cs(int i); static string cs2s(CString s); static void Utf8ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static void Gb2312ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static CString string_to_cstring(string text); static int string_to_int(string str); static string cstring_to_string(CString text); static void Gb2312ToUtf8(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static BOOL IsNumeric(string text); static void UnicodeToGb2312(char* pstrOut, u32 dwOutLen, const wchar_t* pstrIn, u32 dwInLen); static void Utf8ToGb2312(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static int cs2i(CString s); static string int_to_string(int n); static CString ChineseCapitalMoney(double Num); //static wstring s2ws( string s ); static std::wstring s2ws(const string& s); static std::string WChar2Ansi(LPCWSTR pwszSrc); static string ws2s(wstring& inputws); static const char * s2constcharx(string s); static std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen); static char * cs2charx_ansi(CString s); //static string ws2s(wstring ws); };

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧