• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
梦想的天空分外蓝!
展望未来 超越梦想
博客园    首页       联系   管理    订阅  订阅

char*与System::String^的相互转换

#include "stdafx.h"
using namespace System; //这个命名空间如果缺的话,IntPtr这个无法识别
int main(array<System::String ^> ^args)
{
  char* ch1 = "this is chars ";

  //将char*转换为System::String^
  String^ str1= System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)ch1);

  //System::String^转换为char*

  char* ch2 = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str1);
  Console::WriteLine(str1);

  Console::WriteLine(ch2);
  Console::ReadLine();
}

posted @ 2012-10-04 16:48  Kiven  阅读(16698)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3