Re: problem with cstring to char * convertion

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Paul G. Tobey [eMVP] (ptobey)
Date: 10/26/04


Date: Tue, 26 Oct 2004 12:13:35 -0700

WinSock is expecting an array of bytes when you try to send something. So,
if you have a string that you want to send, and that string is in Unicode,
you must convert it to an array of bytes before passing that array, not the
raw CString, to send. There are multiple ways that you might do this and
since we don't know what's on the other end of your socket, we can't tell
you the right way. Here are some of the possibilities:

1. Send the string as Unicode, two bytes per character. You might do this
by simply casting the CString to TCHAR* and then casting that to char* for
send. You'd have to be sure that you specified the byte order, since this
might not be the same on the other end of the socket.

2. You might define what you do as 'convert to ASCII characters'. If that's
the case, you can use WideCharToMultiByte() to convert the contents of your
CString to a char* string (you have to provide a suitable char buffer to
receive the converted string). If you do this, you'll pass the pointer to
the char buffer to send.

Paul T.

"bobby" <bobby@discussions.microsoft.com> wrote in message
news:162B8753-CC82-42FA-AF9D-0229C3D6BCF0@microsoft.com...
> so what do you suggest that i do to convert it.i tried putting _T before
> it
> to get it converted but it complains for that too..
>
>
>
> "Steffen Offermann" wrote:
>
>> > I am getting the warning as follows
>> > C:\project\project\neighbordiscover\neighbordiscoverDlg.cpp(289) :
>> > warning
>> > C4244: '=' : conversion from 'unsigned short' to 'char', possible loss
>> > of
>> > data
>>
>>
>> CStrings are UNICODE under Windows CE.
>>
>>
>>
>>
>>



Relevant Pages

  • Re: Need some assistance with a function
    ... there is much not in those basic books. ... which, will return the array of bytes as a hex string, meaning convert the ... byte array to a string of hex digits that can be printed easily. ... CString MyClass::Bufer2HexString ...
    (microsoft.public.vc.mfc)
  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)
  • Re: passing a string to a dll
    ... Joe, I really appreciate you taking the time to demonstrate this. ... sure how I would implement indexing it for random alphanumeric codes. ... I might handle the array. ... I actually have been wondering if I could use a second string ...
    (microsoft.public.vc.mfc)
  • Re: passing a string to a dll
    ... I might handle the array. ... I actually have been wondering if I could use a second string ... look at insertion cost, organization cost, and search cost. ...
    (microsoft.public.vc.mfc)
  • RE: Structure conversion from C++ to VB-2008?
    ... One of the most important structures is AmiVar structure. ... point number, the array of floating point numbers, a string or IDispatch ... Dim 13012679 as Integer ...
    (microsoft.public.dotnet.languages.vb)