社区
C++ 语言
帖子详情
如何将string型转化为LPCWSTR型
maoyin123456789
2011-04-20 08:48:11
如题,如何将中文的string类型,转化为LPCWSTR型
比如:std::string filename(c:\\我的文档\\作业.doc);如何转化为LPCWSTR型,请高手指教
...全文
1032
11
打赏
收藏
如何将string型转化为LPCWSTR型
如题,如何将中文的string类型,转化为LPCWSTR型 比如:std::string filename(c:\\我的文档\\作业.doc);如何转化为LPCWSTR型,请高手指教
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
打赏
举报
回复
自己先顶个
C++中char*转换为L
PC
WSTR
的解决方案
前言 大家在学习或者使用Windows编程中,经常会碰到字符串之间的转换,char*转L
PC
WSTR
也是其中一个比较常见的转换。下面就列出几种比较常用的转换方法。大家可以根据自己的需求选择相对应的方法,下面来一起学习学习吧。 1、通过MultiByteToWideChar函数转换 MultiByteToWideChar函数是将多字节转换为宽字节的一个API函数,它的原
型
如下: int MultiByteToWideChar( UINT CodePage, // code page DWORD dwFlags, // character-type options L
PC
ST
wince开发用转换类
wince上各种类
型
转换特别是字符串是很头疼的,这个类是项目用的, 每个都测试好用,使用静态方法,直接引用后,写【::cv::就可以用】 typedef int u32; //转换类,所有的转换函数在这里静态 2是to的意思 //cs:c
string
//s:
string
//i:int class cv { public: static char * s2charx(
string
s); //static char * cs2charx_unicode(C
String
s); static char * cs2charx_unicode(C
String
s); static C
String
charx2cs(char *s); cv(); virtual ~cv(); static int c
string
_to_int(C
String
t); static C
String
s2cs(
string
text); static C
String
i2cs(int i); static
string
cs2s(C
String
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 C
String
string
_to_c
string
(
string
text); static int
string
_to_int(
string
str); static
string
c
string
_to_
string
(C
String
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(C
String
s); static
string
int_to_
string
(int n); static C
String
ChineseCapitalMoney(double Num); //static w
string
s2ws(
string
s ); static std::w
string
s2ws(const
string
& s); static std::
string
WChar2Ansi(L
PC
WSTR
pwszSrc); static
string
ws2s(w
string
& inputws); static const char * s2constcharx(
string
s); static std::w
string
Ansi2WChar(L
PC
STR pszSrc, int nLen); static char * cs2charx_ansi(C
String
s); //static
string
ws2s(w
string
ws); };
BSTR详解五
BSTR详解五 - BSTR与其它字符串类
型
转换 1 类
型
转换 常用字符串件的类
型
转换。 From To Sample 字符串常量 BSTR Right: BSTR bs = ::SysAlloc
String
(_T("Test
string
")); … ::SysFree
String
(); Wrong: BSTR bs = _T("Test
string
"); //ERROR LP
WSTR
/ L
PC
WSTR
/ WCHAR* / wchar_t BSTR Right: L
PC
TSTR sz1 = _T("Test
String
"); BSTR bs = ::SysAlloc
String
(sz1); … ::SysFree
String
(); Wrong: LPTSTR sz1 = _T("Test
String
"); BSTR bs = sz1; //ERROR
实战c++中的
string
系列--
string
到L
PC
WSTR
的转换
今天再来介绍一下如何从
string
到L
PC
WSTR
的转换。L
PC
WSTR
是什么类
型
呢? 看看如何定义的:typedef const wchar_t* L
PC
WSTR
;顾名思义就是: L
PC
WSTR
是一个指向unicode编码字符串的32位指针,所指向字符串是wchar
型
,而不是char
型
。比如说MessageBoxW的第二、第三个参数就是L
PC
WSTR
类
型
。`MessageBoxW(__in
L
PC
WSTR
与
string
相互转换
来源互联网,侵删 email:infsine@126.com
string
char*转L
PC
WSTR
L
PC
WSTR
string
ToL
PC
WSTR
(std::
string
orig) { size_t origsize = orig.length() + 1; const size_t newsize = 100; size_t convertedChars = 0; wchar_t ...
C++ 语言
64,654
社区成员
250,484
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章