C++ CLI Byte array to String and back

I'm using sockets in a C++ forms application and it is talking to a native C++ DLL that uses winsock send() recv(). I am needing to convert a array<byte>^ to a String^ as the Socket->Receive() and Socket->Send() methods use a byte array as the buffer for sending and receiving.

I need an explicit process to convert the byte array buffer to a string for the receive, and then do the opposite for going back on the Send side.

Any help would be great...

Thanks,

T-
thanks for the link modoran...

i used the method for conversion shown in the topic however I get a garbled mess of characters on the other end after sending:

the code i used to convert is:
1
2
3
4
5
String^ converter=gcnew String("Got the message\0");

array<Byte>^ data = gcnew array<Byte>(converter->Length);
			 System::Runtime::InteropServices::Marshal::Copy(IntPtr(&converter), data, 0, converter->Length);
			 this->serverSocket->Send(data,data->Length,SocketFlags::None);


on the receiving end the process is unmanaged and uses winsock:
1
2
3
4
5
6
7
8
9
10
char buffer[1000];
    memset(buffer, 0, 999);
    
    int msgl=recv(c_sock,buffer,1000,0);
    if (msgl==-1)
    {
            msgl=WSAGetLastError();
            cout<<msgl<<endl;
    }
    cout<<buffer<<endl;


I receive the correct number of bytes but the original string ""Got the message\0" ends up as a mess of characters...

any advice on how to remedy this...I'll bet i'm missing something pretty simple.

Thnaks,
T-
I figured it out...by using a simple NetworkStream and encoding the byte arrays i can send to and receive from the native winsock using .net sockets...
Topic archived. No new replies allowed.