String* 转 char*出错,请高手指点!!!急

yxbyxbwj 2004-09-12 02:01:47
//String -> char*
char* Socket_Cl::stoc(String *s)
{
char* c = "";
memset(c,0x00,100);

Byte bytes[] = System::Text::Encoding::ASCII->GetBytes(s);

for ( int i = 0 ; i < s->get_Length(); i++ ){
c[i] = bytes[i];
}

return c;
}
...全文
190 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoleixinhua 2004-09-12
  • 打赏
  • 举报
回复
Pointer "c" need "new" to alloc memory.
saucer 2004-09-12
  • 打赏
  • 举报
回复
you didn't alloc memory for "c" variable

if you want __wchar_t [], you can just do

s->ToCharArray();

otherwise, look into System::Runtime::InteropServices::Marshal class' StringToHGlobalAnsi method

String __gc* str = S"managed string";
const char __nogc* pStr = static_cast<const char*>(Marshal::StringToHGlobalAnsi(str).ToPointer());


also see
http://www.thecodeproject.com/dotnet/managed_unmanaged.asp

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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