.NET 中的二进制数据? (C++/CLI)

发布于 2024-07-12 19:27:29 字数 260 浏览 5 评论 0原文

在 .NET 中存储二进制数据的首选方式是什么?

我已经尝试过这个:

byte data __gc [] = __gc new byte [100];

并收到此错误:

error C2726: '__gc new' may only be used to create an object with managed type

有没有办法管理字节数组?

What's the prefered way to store binary data in .NET?

I've tried this:

byte data __gc [] = __gc new byte [100];

And got this error:

error C2726: '__gc new' may only be used to create an object with managed type

Is there a way to have managed array of bytes?

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

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

发布评论

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

评论(3

甜味拾荒者 2024-07-19 19:27:36

我不知道这样做的首选方法。 但如果您只想编译它,以下是我的计算机上的 C++/CLI CLRConsole 项目的工作代码。

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    cli::array<System::Byte^>^ a = 
        gcnew cli::array<System::Byte^>(101);

    a[1] = (unsigned char)124;

    cli::array<unsigned char>^ b = 
        gcnew cli::array<unsigned char>(102);

    b[1] = (unsigned char)211;

    Console::WriteLine(a->Length);
    Console::WriteLine(b->Length);

    Console::WriteLine(a[1] + " : " + b[1]);
    return 0;
}

输出:

101
102
124 : 211

a 是托管字节的托管数组。 b 是无符号字符的托管数组。 C++ 似乎没有内置 byte 数据类型。

I don't know the preferred way of doing this. But if you only want it compiled, the following are working code from my C++/CLI CLRConsole project from my machine.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    cli::array<System::Byte^>^ a = 
        gcnew cli::array<System::Byte^>(101);

    a[1] = (unsigned char)124;

    cli::array<unsigned char>^ b = 
        gcnew cli::array<unsigned char>(102);

    b[1] = (unsigned char)211;

    Console::WriteLine(a->Length);
    Console::WriteLine(b->Length);

    Console::WriteLine(a[1] + " : " + b[1]);
    return 0;
}

Output:

101
102
124 : 211

a is managed array of managed byte. And b is managed array of unsigned char. C++ seems not to have byte data type built in.

木槿暧夏七纪年 2024-07-19 19:27:35

CodeProject: C++/CLI 中的数组

据我所知 '__gc new'语法已弃用,请尝试以下操作:

cli::array<byte>^ data = gcnew cli::array<byte>(100);

我注意到您在使用 cli 命名空间时遇到问题。 阅读MSDN 上的命名空间来解决您的问题问题。

CodeProject: Arrays in C++/CLI

As far as I know '__gc new' syntax is deprecated, try following:

cli::array<byte>^ data = gcnew cli::array<byte>(100);

I noted that you're having problems with cli namespace. Read about this namespace on MSDN to resolve your issues.

烦人精 2024-07-19 19:27:34

您使用的是托管 C++ 还是 C++/CLI? (我可以看到 Jon Skeet 编辑了问题以将 C++/CLI 添加到标题中,但对我来说,您实际上正在使用托管 C++)。

但无论如何:

在托管 C++ 中,您可以这样做:

Byte data __gc [] = new Byte __gc [100];

在 C++/CLI 中,它看起来像这样:

cli::array<unsigned char>^ data = gcnew cli::array<unsigned char>(100);

Are you using Managed C++ or C++/CLI? (I can see that Jon Skeet edited the question to add C++/CLI to the title, but to me it looks like you are actually using Managed C++).

But anyway:

In Managed C++ you would do it like this:

Byte data __gc [] = new Byte __gc [100];

In C++/CLI it looks like this:

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