本文介绍了字符串*到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 ==]

解决方案




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

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


#include< vcclr.h>


wchar_t __pin * p = PtrToStringChars(my_string);


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


问候,






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

#include< vcclr.h>

wchar_t __pin * p = PtrToStringChars(my_string);

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







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==]

解决方案



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




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





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 []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:58