std :: string - > WCHAR * [英] std::string -> WCHAR*

查看:60
本文介绍了std :: string - > WCHAR *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将std :: string转换为WCHAR *?


有什么方法或东西吗?我找不到。谢谢

How to convert a std::string to a WCHAR* ?

is there any methods or something ? I can''t find. Thanks

推荐答案



" Flzw" < FL **** @ wanadoo.fr>在消息中写道

news:cg ********** @ news-reader3.wanadoo.fr ...

"Flzw" <fl****@wanadoo.fr> wrote in message
news:cg**********@news-reader3.wanadoo.fr...
如何转换std ::字符串到WCHAR *?

是否有任何方法或东西?我找不到。谢谢
How to convert a std::string to a WCHAR* ?

is there any methods or something ? I can''t find. Thanks




好​​吧,我试过使用

typedef std :: basic_string< WCHAR> wstring;


然后我会用c_str()来获取WCHAR *


程序编译好但是它崩溃了

wstring wstr = L" Test";


它似乎触发了一个错误的分配并在内部strlen的某处崩溃

call


我知道我可以通过获取c_str()然后

将它复制到带有std:copy的ushort数组来转换基本的std:string但是因为它是经常被称为

函数,我宁愿让它快速,使用WCHAR来构建字符串

很棒。



Well, I tried to use
typedef std::basic_string<WCHAR> wstring;

and then I would use c_str() to get WCHAR*

the program compiles fine but it crashes on
wstring wstr = L"Test";

it seems to trigger a bad alloc and crash somewhere in an internal strlen
call

I know I can convert a basic std:string by getting the c_str() and then
copying it to a ushort array with std:copy but as it is an often called
function, I''d rather want it to be fast, using WCHAR to build strings would
be great.


Le dimanche 29 ao?t2004à11:46:47,Flzwaécritdanscomp.lang.c ++ *:
Le dimanche 29 ao?t 2004 à 11:46:47, Flzw a écrit dans comp.lang.c++*:
如何转换std :: string到WCHAR *?

是否有任何方法或东西?我找不到。谢谢
How to convert a std::string to a WCHAR* ?

is there any methods or something ? I can''t find. Thanks




我能看到的最接近标准的方式是:


#include< cstdlib>

#include< string>

....

const std :: string input =" string to conversion" ;;


// null-call获取大小

size_t needed = :: mbstowcs(NULL,& input [0],input.length()) ;


//分配

std :: wstring输出;

output.resize(需要);
< br $>
//真实电话

:: mbstowcs(& output [0],& input [0],input.length());


//你问了一个指针

wchar_t * pout = output.c_str();


-

___________ 2004-08-29 12:47:10

_ / _ \_`_`_`_)Serge PACCALIN - sp ad mailclub.net

\ \ _L_)Il faut donc que les hommes startsnt

- ''(__)parn''êtrepasfanatiquespourmériter

_ / ___( la)tolérance。 - Voltaire,1763



The closest-to-standard way I can see is:

#include <cstdlib>
#include <string>
....

const std::string input = "string to convert";

// null-call to get the size
size_t needed = ::mbstowcs(NULL,&input[0],input.length());

// allocate
std::wstring output;
output.resize(needed);

// real call
::mbstowcs(&output[0],&input[0],input.length());

// You asked for a pointer
wchar_t *pout = output.c_str();

--
___________ 2004-08-29 12:47:10
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-''(__) par n''être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763


On Sun,2004年8月29日11:46:47 +0200,Flzw < FL **** @ wanadoo.fr>写道:
On Sun, 29 Aug 2004 11:46:47 +0200, "Flzw" <fl****@wanadoo.fr> wrote:
如何将std :: string转换为WCHAR *?
是否有任何方法或东西?我找不到。谢谢
How to convert a std::string to a WCHAR* ?
is there any methods or something ? I can''t find. Thanks




< OT-rant>

WCHAR是一种非标准的Windows类型。因此,它在

comp.lang.c ++中是偏离主题的,它仅涉及ANSI和ISO标准

C ++语言主题。请阅读以下新闻组的常见问题解答:
http:// www.parashift.com/c++-faq-lite/

< / OT-rant>


尽管如此,你仍然坚持使用std :: string并且需要知道

要做什么。这取决于std :: string'的字符数据是如何编码的,即使用哪个区域设置或代码页。


Fr starters,I将假设您知道有几种不同的
Unicode编码( http:/ /www.unicode.org)。 WCHAR被定义为

16位Unicode字符。这只是其中之一。


对于来自西欧代码集的数据(即ISO-8859-1

或ISO-8859-15) ,转换是微不足道的,因为MSB(大多数
有效字节)始终为0.你需要提供一个WCHAR的缓冲区,

这个大到足以包含所有的字符数据加一个

终止空WCHAR,只是复制

字符串中的字符。不要使用memcpy等,但是使用循环并将字符

按字符复制到缓冲区中。如果动态分配缓冲区,请确保通过调用delete [](如果你使用new [])

完成后释放内存,否则使用一个智能指针,可以处理数组

数据(std :: auto_ptr<>不能......在

Boost库中有这样的智能指针,但是:http: ://www.boost.org)。


由于您的帖子似乎来自法国,请注意欧元

字符? Unicode编码为0x20ac,因此对于此特定字符,MSB为
*而不是* 0。在Windows中,它定义为

0x80,这是(AFAIK)ISO-8859-1代码中不可打印的字符

页。

如果涉及不同的代码集,您将需要使用

您用于开发的框架提供的库

(例如MFC) ,Borland上的VCL)或者使用适合的Windows API函数




你的编译器可能已经实现了一些方便的方面,这些方便了一些

通过std :: [w] iostream转换,例如退房:


std :: ios_base :: imbue(const std :: locale&))。


请注意一些库函数对于Windows上的COM / OLE期望BSTR

参数,或者返回BSTR,这是一种非标准的OLE数据类型

虽然我认为它被定义为WCHAR * 。这是一款完全不同的颜色,并且存在内存管理问题

这些是特定于操作系统的。如果这是你需要的,那就去一个

微软新闻组来处理Windows特定的C ++

开发。


-

Bob Hairgrove
否******** **@Home.com



<OT-rant>
WCHAR is a non-standard Windows type. Therefore, it is off-topic in
comp.lang.c++ which concerns itself only with ANSI and ISO standard
C++ language topics. Please read the FAQ for this newsgroup at:
http://www.parashift.com/c++-faq-lite/
</OT-rant>

Nevertheless, you are stuck with a std::string and need to know what
to do with it. It depends on how the std::string''s character data is
encoded, i.e. which locale or code page is used.

Fr starters, I will assume you know that there are several different
Unicode encodings (http://www.unicode.org). WCHAR is defined as a
"16-bit Unicode character" which is only one of them.

For data which comes from Western European code set (i.e. ISO-8859-1
or ISO-8859-15), the conversion is trivial since the MSB (most
significant byte) is always 0. You need to supply a buffer of WCHAR,
which is large enough to contain all the character data plus one
terminating null WCHAR, and merely copy the characters from the
string. Don''t use memcpy or such, but use a loop and copy it character
by character into the buffer. If the buffer is allocated dynamically,
be sure to release the memory by calling delete[] (if you used new[])
when you are done, or else use a smart pointer which can handle array
data (std::auto_ptr<> cannot ... there are such smart pointers in the
Boost library, though: http:://www.boost.org ).

Since your post seems to originate in France, be aware that the Euro
character "?" has a Unicode encoding of 0x20ac, therefore the MSB is
*not* zero for this particular character. In Windows, it is defined as
0x80 which is (AFAIK) a non-printable character in ISO-8859-1 code
page.

If there is a different code set involved, you will need to use either
the libraries supplied by the framework you are using for development
(e.g. MFC, VCL on Borland) or use the Windows API functions suitable
for this.

Your compiler may have implemented locale facets which facilitate some
conversions via std::[w]iostream, e.g. check out:

std::ios_base::imbue(const std::locale &)).

Note that some library functions for COM/OLE on Windows expect a BSTR
argument, or return a BSTR, which is a non-standard OLE data type
although I believe it is defined as WCHAR*. This is a horse of an
entirely different color, and there are issues with memory management
which are OS-specific. If this is what you need, go to one of the
Microsoft newsgroups which deals with Windows-specific C++
development.

--
Bob Hairgrove
No**********@Home.com


这篇关于std :: string - &gt; WCHAR *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆