Convert char* to char __gc[] 
Author Message
 Convert char* to char __gc[]

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);

Quote:
}

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



Tue, 06 Dec 2005 05:12:04 GMT  
 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



Tue, 06 Dec 2005 06:27:06 GMT  
 Convert char* to char __gc[]
Of course the line :

char* ptr = {abcdefghijklmnopqrs"};

should read:
char* ptr = {"abcdefghijklmnopqrs"};



Quote:
> 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;
>         }
>     }
> };

> int main()
> {
>     test *my = new test();
>     char* ptr = {abcdefghijklmnopqrs"};
>     my->mymain(ptr);
>     return 0;
> }

> Stephen Fraser
> Managed C++ and .NET Development



> > 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



Tue, 06 Dec 2005 06:33:36 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Converting char array of literals chars to escape chars

2. Newbie: Howto convert char variable __gc into CString?

3. cannot convert from char* to char[4]

4. Convert four chars to a long and a long to four chars

5. Converting unsigned char to signed char invokes UB?

6. converting char to pointer char

7. Convert an 8bit char in a 7bit char!

8. converting const char* to unsigned char[1024]

9. Convert from char to unsigned char

10. convert char to const char

11. Any way to convert char (*) [256] to char * []?

12. converting a char* to char[10]

 

 
Powered by phpBB® Forum Software