数组^ 和 byte* 之间有什么区别?

发布于 2024-11-06 03:42:30 字数 303 浏览 4 评论 0原文

是否可以强制转换数组<字节>^ 到字节*?

下面的代码需要如何更改才能返回一个字节*?

array<Byte>^ StrToByteArray(System::String^ unicodeString)
{
    UTF8Encoding^ utf8 = gcnew UTF8Encoding;
    array<Byte>^ encodedBytes = utf8->GetBytes( unicodeString );
    return encodedBytes;
}

And is it possible to cast the array< Byte>^ to an byte*?

how would the below code need to changed to return a byte*?

array<Byte>^ StrToByteArray(System::String^ unicodeString)
{
    UTF8Encoding^ utf8 = gcnew UTF8Encoding;
    array<Byte>^ encodedBytes = utf8->GetBytes( unicodeString );
    return encodedBytes;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷迭香的记忆 2024-11-13 03:42:30

array^ 是托管堆中对象的句柄,byte* 是指向非托管字节的指针。您不能在它们之间进行转换,但可以修复托管数组并获取指向其中元素的指针。

编辑回应第一条评论:

这是取自msdn 上的此页面

您最感兴趣的是 void Load() 方法。在这里,他们固定数组,并获取指向其中第一个元素的指针......

// pin_ptr_1.cpp
// compile with: /clr 
using namespace System;
#define SIZE 10

#pragma unmanaged
// native function that initializes an array
void native_function(byte* p) {
    for(byte i = 0 ; i < 10 ; i++)
        p[i] = i;
}
#pragma managed

public ref class A {
private:
    array<byte>^ arr;   // CLR integer array

public:
    A() {
        arr = gcnew array<byte>(SIZE);
    }

    void load() {
        pin_ptr<byte> p = &arr[0];   // pin pointer to first element in arr
        byte* np = p;   // pointer to the first element in arr
        native_function(np);   // pass pointer to native function
    }

    int sum() {
        int total = 0;
        for (int i = 0 ; i < SIZE ; i++)
            total += arr[i];
        return total;
    }
};

int main() {
    A^ a = gcnew A;
    a->load();   // initialize managed array using the native function
    Console::WriteLine(a->sum());
}

array^ is a handle to an object in the managed heap, byte* is a pointer to an unmanaged byte. You cannot cast between them, but it is possible to fix the managed array and obtain a pointer to the elements within it.

EDIT in response to first comment:

Here's a code sample taken from this page on msdn

The bit you are most interested in is the void Load() method. Here they are pinning the array, and taking a pointer to the first element in it...

// pin_ptr_1.cpp
// compile with: /clr 
using namespace System;
#define SIZE 10

#pragma unmanaged
// native function that initializes an array
void native_function(byte* p) {
    for(byte i = 0 ; i < 10 ; i++)
        p[i] = i;
}
#pragma managed

public ref class A {
private:
    array<byte>^ arr;   // CLR integer array

public:
    A() {
        arr = gcnew array<byte>(SIZE);
    }

    void load() {
        pin_ptr<byte> p = &arr[0];   // pin pointer to first element in arr
        byte* np = p;   // pointer to the first element in arr
        native_function(np);   // pass pointer to native function
    }

    int sum() {
        int total = 0;
        for (int i = 0 ; i < SIZE ; i++)
            total += arr[i];
        return total;
    }
};

int main() {
    A^ a = gcnew A;
    a->load();   // initialize managed array using the native function
    Console::WriteLine(a->sum());
}
揽月 2024-11-13 03:42:30

不,你不能转换它,但 array 有一个方法可以返回原始数组,称为“data()”

编辑:nvm,以为你在谈论 stl 类数组。

No, you cannot cast it, but array has a method that returns the raw array called "data()"

EDIT: nvm, thought you were talking about the stl class array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文