CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2006
    Posts
    8

    Question 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

  2. #2
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: wchar[] to char[] and vice versa

    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...
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  3. #3
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    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.

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    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

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: wchar[] to char[] and vice versa

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Lightbulb 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);
    Last edited by Maximus_X; September 26th, 2006 at 02:16 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured