S
S
sddvxd2018-02-24 14:55:25
C++ / C#
sddvxd, 2018-02-24 14:55:25

How to convert wstring to char* and vice versa?

Good afternoon! I'm currently learning about sockets and ran into a problem

wstring strTheNameOfTheFile = FindFileData.cFileName;
char* serial = (char*)strTheNameOfTheFile.c_str();
iResult = send(ConnectSocket, serial, (int)strlen(serial), 0);

:
here I am passing a unicode string converted to char* to the
server:
char* echo = new char[1024];
      
      do {

        iResult = recv(AcceptSocket, echo, 1024, 0);
        if (iResult > 0  && iResult == 1024) {
          FILE * pFile;

          stringstream ss;
          ss << counter << echo;
          string str(ss.str());
          cout << iResult<<endl;
          
          pFile = fopen(str.c_str(), "ab");

          fwrite(echo, iResult, 1, pFile);
          fclose(pFile);
        }
          
        else if (iResult < 1024 && iResult != 0) {

          FILE * pFile;

          stringstream ss;
          ss << counter << echo;
          string str(ss.str());

          wstring ws(&echo[0], &echo[(int)strlen(echo)]);
          wcout << ws << endl;

          pFile = fopen(str.c_str(), "ab");

          fwrite(echo, iResult, 1, pFile);
          fclose(pFile);

          counter++;
          int sendD = send(AcceptSocket, sendC, 3, 0);
          
        }
        else
          cout << "Disconnect..." << endl;

      } while (iResult > 0);

here I am trying to accept ansii and convert to unicode
please help me figure out how to do the conversion correctly. I
apologize for repeating the code or using obsolete C functions for managing characters. With understand the characters until everything is bad

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-02-24
@res2001

If we are talking about Windows, then there are API functions for converting: WideCharToMultiByte/MultiByteToWideChar
Under Linux, you probably need to use some library or convert it yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question