Binary data in .NET? (C ++ / CLI)

What is the preferred way to store binary data in .NET?

I 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 manage an array of bytes?

+4
source share
3 answers

Are you using managed C ++ or C ++ / CLI? (I see that Jon Skeet has edited the question of adding C ++ / CLI to the header, but to me it looks like you are actually using Managed C ++).

But anyway:

In managed C ++, you do this as follows:

 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); 
+3
source

CodeProject: Arrays in C ++ / CLI

As far as I know, the syntax of "__gc new" is deprecated, try the following:

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

I noticed that you have problems with the cli namespace. Read about this namespace on MSDN to solve your problems.

+2
source

I do not know how to do that. But if you want it to be compiled, the following 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 a managed byte array. And b is the unsigned char managed array. C ++ doesn't seem to have a built-in byte data type.

+1
source

All Articles