如何将 String^ 转换为 char 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13665649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:44:17  来源:igfitidea点击:

How to convert String^ to char array

stringvisual-c++c++-clitype-conversion

提问by user1869623

Possible Duplicate:
Need to convert String^ to char *

可能的重复:
需要将 String^ 转换为 char *

I have been looking so long for this solution but I can't find nothing specific. I work in Visual Studio C++, windows forms app. I need to convert String^value into char array. I have stored value from TextBoxin String^:

我一直在寻找这个解决方案,但我找不到任何具体的东西。我在 Visual Studio C++、windows 窗体应用程序中工作。我需要将String^值转换为字符数组。我已经从TextBoxin存储了价值String^

String^ target_str = targetTextBox->Text;

// lets say that input is "Need this string"

I need to convert this String^and get output similar to this:

我需要转换它String^并获得与此类似的输出:

char target[] = "Need this string";

If it is defined as char target[]it works but I want to get this value from TextBox.

如果它被定义为char target[]有效但我想从TextBox.

I have tried marshaling but it didn't work. Is there any solution how to do this?

我试过编组,但没有奏效。有什么解决办法吗?

I have found how to convert std::stringto chararray so another way how to solve this is to convert String^to std::stringbut I have got problems with this too.

我已经找到了如何转换std::stringchar数组,所以另一种解决这个问题的方法是转换String^为,std::string但我也遇到了这个问题。

回答by Maurice Reeves

Your best bet is to follow the examples set forth in this question.

最好的办法是遵循这个问题中列出的例子。

Here's some sample code:

下面是一些示例代码:

String^ test = L"I am a .Net string of type System::String";
IntPtr ptrToNativeString = Marshal::StringToHGlobalAnsi(test);
char* nativeString = static_cast<char*>(ptrToNativeString.ToPointer());

The reason for this is because a .Net string is obviously a GC'd object that's part of the Common Language Runtime, and you need to cross the CLI boundary by employing the InteropServices boundary. Best of luck.

这样做的原因是因为 .Net 字符串显然是一个 GC 对象,它是公共语言运行时的一部分,您需要通过使用 InteropServices 边界来跨越 CLI 边界。祝你好运。

回答by Pragmateek

In C/C++ there is equivalence between char[] and char* : at runtime char[] is no more than a char* pointer to the first element of the array.

在 C/C++ 中, char[] 和 char* 是等价的:在运行时 char[] 只不过是指向数组第一个元素的 char* 指针。

So you can use you char* where a char[] is expected :

因此,您可以在需要 char[] 的地方使用 char* :

#include <iostream>
using namespace System;
using namespace System::Runtime::InteropServices;

void display(char s[])
{
    std::cout << s << std::endl;
}

int main()
{
    String^ test = L"I am a .Net string of type System::String";
    IntPtr ptrToNativeString = Marshal::StringToHGlobalAnsi(test);
    char* nativeString = static_cast<char*>(ptrToNativeString.ToPointer());
    display(nativeString);
}

So I think you can accept Maurice's answer :)

所以我认为你可以接受莫里斯的回答:)