wchar[] to char[] and vice versa
Hi ,
I m struck up in a query . where i have to convert the wchar[] to char[] in vc++6.0 and pass the buffer over the TCP socket .
it is like this :
Client:-
CString csQuery=_T("C:\\精选图象件夹夹夹 \\ 精选图.txt"); //UTF-8
wchar_t wbuff[512];
for(int i=0;i<csQuery.GetLength();i++)
wbuff[i]=csQuery[i];
wbuff[i]='\0';
char buff[512];
--here i need to convert wbuff[] to char[] and pass it over the socket
int iSend=send(clisock, buff,sizeof(buff),0);
--
--
--
Server:-
here in server side, ill receive the buffer and need to convert the same to wide char (wchar_t[])
iRecv=recv(msgsock[id],buff,512,0);
---convert buff to wbuff
How can i do it?????
I have tried MultibytetoWideChar , WideChartoMultiByte but i dont know why it is not coming
Pls help me
Re: wchar[] to char[] and vice versa
Quote:
I have tried MultibytetoWideChar , WideChartoMultiByte but i dont know why it is not coming
This should work... post your code that you have tried with... somebody could help you with that...
Re: wchar[] to char[] and vice versa
Some questions:
why do you need to convert from wchar to char ? My understanding from your explanation seems like it is not what you really want. All you want is to transfer this wchar data throught the socker and interpret it likewise at the receiving end. If that is true, do NOT use MultiByteToWideChar family of functions. Instead , simply cast your WCHAR array as a (BYTE*) and send it across the socket. At the receiving end, do something similar. Just accept the incoming message as a BYTE* and then, memcpy that over to a WCHAR directly.
Please forgive me if I understood you wrong.
Re: wchar[] to char[] and vice versa
As kirant Suggest just use BYTE * here.Second length of wbuff array is Fixed.how you Know that you are Not Going to take more then 512 Character in your buffer better .instead of 512 use CString Getlength() Function to allocate proper size to your Array.Even i think instead of WCHAR you simply can go with BYTE array use memcpy to copy the array to your char array and Simply sen it to your Socket.
Let me Know if you want Something else. or if i am wrong.
Thanx
Re: wchar[] to char[] and vice versa
Re: wchar[] to char[] and vice versa
I understand that you want to send a char buffer from a client to a server.
I don't understand why you are using wchar....
There is a CT2A macro that enable to convert from a CString to a char pointer.
Code:
void SendString(CString strToSend)
{
// === initialize socket connection
//** sending and receive the data
int bytesSent;
CT2A sendbuf(strToSend); // CString to char * conversion --- that's all that you need ;)
bytesSent = send (m_socket, sendbuf, 82, 0);
// === Close the socket connection
}
And in your server code you need to have in the receiving loop:
Code:
char recvbuf[90] = "";
int bytesRecv = SOCKET_ERROR;
// receinving data
bytesRecv = recv( acceptSocket, recvbuf, 82, 0);
:thumb: