
Convert char* to char __gc[]
The code you provided doesn't generate an error. I guess I must be missing
something.
I rewrote it so that I could test it:
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
__gc class test
{
public:
void foo (Byte x[])
{
for (int i = 0; i < 4; i++)
Console::WriteLine(x[i]);
}
void mymain(char *ptr)
{
int offset = 0;
for (int i=1; i<5; ++i)
{
Byte bytes[] =
System::Text::ASCIIEncoding::Default->GetBytes(ptr + offset);
foo(bytes);
offset += i;
}
}
Quote:
};
int main()
{
test *my = new test();
char* ptr = {abcdefghijklmnopqrs"};
my->mymain(ptr);
return 0;
Quote:
}
Stephen Fraser
Managed C++ and .NET Development
Quote:
> Is there a quick way to convert an unmanaged char* to a managed byte
> array?
> I have some code like the following:
> int getOffset(int);
> void foo(Byte[]);
> char* ptr = .....;
> int offset = 0;
> for (int i=0; i<N; ++i)
> {
> Byte bytes[] = System::Text::ASCIIEncoding::Default->GetBytes(ptr +
> offset);
> foo(bytes);
> offset += getOffset(i);
> }
> Loops thru a char[] and calls a function foo that requires a Byte[]
> arg (managed array), each time while doing pointer arithmetic.
> Basically the above results in the following error:
> "cannot convert parameter 1 from char * to _wchar_t __gc[]."
> So how do I remedy the above? Ofcourse, what I am really trying to
> accomplish is to do some kind of pointer type arithmetic on a Byte[].
> MR