c++中把char * 和System::String 互转

将char [] 转换成System::String

 

     char name[10]=”abcdefg”;

     System::String^ str_name;

   str_name = gcnew String(name);

将System::String 转换成char *

#include “stdafx.h”

#include <iostream>
#include <string>
#include <vcclr.h>
using System::String;
void stoc(System::String^ str,char *nstring)
{
pin_ptr <const wchar_t> wch = PtrToStringChars(str);

// Convert to a char*
size_t origsize = wcslen(wch) + 1;
size_t convertedChars = 0;
wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
}

前面有些头文件是不必要的

0 条评论

What is 10 + 2 ?
Please leave these two fields as-is:
不答对这道小题,是不给通过的哦

昵称

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

沙发空缺中,还不快抢~