字符串*到char [] [英] String* to char[]

查看:107
本文介绍了字符串*到char []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将String *转换为char []?具体来说:


String * my_string =" HELLO" ;

char m_char_array [32 + 1];


m_char_array = my_string; //错误

strcpy(m_char_array,my_string); //错误

strcpy(m_char_array,my_string.ToCharArray()); //错误

strcpy(m_char_array,my_string.ToCharArray(0,32)); //错误

strcpy(m_char_array,my_string.ToCharArray(0,0)); //错误


提前致谢! :)


[== Peteroid ==]

解决方案

" Peteroid" < PE ************ @ msn.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

如何heck是否将String *转换为char []?具体来说:

String * my_string =" HELLO" ;
char m_char_array [32 + 1];
...




首先固定刺痛物体(防止它被GC移动) ),并获得

a指向字符串的第一个字符


#include< vcclr.h>


wchar_t __pin * p = PtrToStringChars(my_string);


然后获得尽可能多的角色,将它们复制到目标


问候,




您好,


感谢您的回复。以下行:


wchar_t __pin * p = PtrToStringChars(my_string);


现在生成错误:p是未声明的标识符(错误C2065)。我确实包含了''vcclr.h''包含的
。我以前从来没有使用过pinning,虽然我很清楚它是什么意思(GC可以在内存中移动东西,pin

防止这种情况,只是没有当这是必要的,当它不是时)确定。

另外,我正在尝试将结果放入char [33]数组中,因此wchar_t *

与此兼容以进行直接复制吗?


另外,当你说''将它们复制到目标''(即char [33]变量)可以

你给我一个如何做到这一点的想法(例如,使用strcpy?)。再次感谢! :)


[== Peteroid ==]


PS - String * my_string =" HELLO"这很奇怪;工作(即直接

从字符串到字符串的赋值)但反过来(可能是

因为char来自C *(+)中的String *之前laguage development)。但是它确实不应该将char []复制到一个字符串

(恕我直言)这样复杂,因为要求这样的转换变旧代码使用

char []更容易现代化(即String应该带有一个内置的

转换工具,我认为这是ToCharArray()的目的(其中

好​​像不应该做它应该做的事情,因为它输出的内容

不像char [])......


" William DePalo [MVP VC ++]< wi *********** @ mvps.org>写在消息中

新闻:OE ************* @ TK2MSFTNGP14.phx.gbl ...

" Peteroid"< pe ************ @ msn.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

如何heck是否将String *转换为char []?具体来说:

String * my_string =" HELLO" ;;
char m_char_array [32 + 1];
...


首先固定刺痛物体(防止它被GC移动),并获得指向弦的第一个字符的指针

#include< vcclr.h>

wchar_t __pin * p = PtrToStringChars(my_string);

然后获取尽可能多的角色,将它们复制到目标
<问候,




Peteroid写道:

如何heck是否将String *转换为char []?具体来说:

String * my_string =" HELLO" ;




HGLOBAL hg =(HGLOBAL)System :: Marshal :: StringToHGlobalAnsi(my_s tring);

char * p = GlobalLock(hg );


//使用p - 它是一个指向零终止字符串的普通指针

GlobalFree(hg);


-cd


How the heck does one convert a String* to char[]? Specifically:

String* my_string = "HELLO" ;
char m_char_array[32+1] ;

m_char_array = my_string ; // error
strcpy( m_char_array, my_string ) ; // error
strcpy( m_char_array, my_string.ToCharArray( ) ) ; // error
strcpy( m_char_array, my_string.ToCharArray(0,32 ) ) ; // error
strcpy( m_char_array, my_string.ToCharArray(0,0) ) ; // error

Thanks in advance! : )

[==Peteroid==]

解决方案

"Peteroid" <pe************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

How the heck does one convert a String* to char[]? Specifically:

String* my_string = "HELLO" ;
char m_char_array[32+1] ;
...



First pin the sting object (prevent it from being moved by the GC), and get
a pointer to the first character of the string

#include <vcclr.h>

wchar_t __pin *p = PtrToStringChars(my_string);

then for as many characters as you have got, copy them to the target

Regards,
Will


Hi Will,

Thanks for the reply. The following line:

wchar_t __pin *p = PtrToStringChars(my_string);

now generates the error: "p is an undeclared indentifier (error C2065)". I
did include the ''vcclr.h'' include. I''ve never used pinning before, though I
vaguely know what it''s for (GC can move things around in memory, pin
prevents this, just not sure yet when this is necessary and when it isn''t).
Also, I''m trying to get the result into a char[33] array, so is wchar_t*
compatible with this for direct copying?

Also, when you say ''copy them to target'' (i.e., the char[33] variable) could
you give me an idea how to do this (e.g., use strcpy?). Thanks again! :)

[==Peteroid==]

PS - It''s weird that String* my_string = "HELLO" ; works (i.e. direct
assignment from a char string to a String) but the reverse doesn''t (probably
because char came before String* in C(++) laguage development). But it
really shouldn''t be this complicated to get a char[] copied to a String
(IMHO), as the requirement for such a conversion to make old code using
char[] more easily modernizable (i.e., String should have come with a built
in conversion tool, which I thought was the purpose of ToCharArray() (which
seems to not do what it looks like it should be doing, as what it outputs
does not act like a char[])...

"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message
news:OE*************@TK2MSFTNGP14.phx.gbl...

"Peteroid" <pe************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

How the heck does one convert a String* to char[]? Specifically:

String* my_string = "HELLO" ;
char m_char_array[32+1] ;
...



First pin the sting object (prevent it from being moved by the GC), and
get a pointer to the first character of the string

#include <vcclr.h>

wchar_t __pin *p = PtrToStringChars(my_string);

then for as many characters as you have got, copy them to the target

Regards,
Will



Peteroid wrote:

How the heck does one convert a String* to char[]? Specifically:

String* my_string = "HELLO" ;



HGLOBAL hg = (HGLOBAL)System::Marshal::StringToHGlobalAnsi(my_s tring);
char* p = GlobalLock(hg);

// work with p - it''s an ordinary pointer to zero-terminated string

GlobalFree(hg);

-cd


这篇关于字符串*到char []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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