Home > Software engineering >  MFC in socket programming, under the unicode data is received garbled, how to solve
MFC in socket programming, under the unicode data is received garbled, how to solve

Time:09-30

In unicode, consult everybody, fairy,

CodePudding user response:

How hair is how close, general network transmission in utf-8, internationally

CodePudding user response:

The
refer to the original poster wjx152615 response:
to under the unicode, consult everybody, fairy,
how to use utf-8, just learning is not very will

CodePudding user response:

Receiving part
Char szTemp [200].
Int n=Receive (szTemp, 200);
SzTemp [n]=0;
Cstrings sTemp.
STemp. The Format (_T (" % s "), szTemp);

CodePudding user response:

Send some
The UpdateData ();
M_ClientSocket. Send (m_sWords, m_sWords GetLength ());//message
M_ListWords. AddString (_T (" send: ") + m_sWords);

CodePudding user response:

Computer memory or file content or transport it is just a one-dimensional binary byte array and its corresponding binary address;
The human brain to a computer memory or file contents or transfer the content of the one-dimensional binary byte array and its corresponding binary address some parts as an integer, number of signed/unsigned number, floating point Numbers, complex Numbers, letters, digits, Chinese/Korean/French... Character/string, assembly instructions, functions, function parameters, heap, stack, arrays, Pointers, array pointer and pointer array, the array of arrays, pointer to pointer, two-dimensional arrays, character lattice, the coordinates of character strokes, black and white binary images and grayscale images, color images, audio, video, fingerprint information, id information...

For computer no noise, only the binary bytes; To the human brain is gibberish, GBK: 0 xb0 0 xa1, Unicode - 16 LE: 0 x4a 0 x55, Unicode - 16 BE: 0 x55 0 x4a, utf-8 8-0 x95 xE5 0 0 x8a

 void HexDump) (char * buf, int len, int addr) {
Int I, j, k;
Char binstr [80].

for (i=0; IIf (0==16) (I %) {
Sprintf (binstr, "% x - 08", I + addr);
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
} else if (15==16) (I %) {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
Sprintf (binstr, "% s", binstr);
For (j=I - 15; J<=I; J++) {
Sprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
Printf (" % s \ n ", binstr);
} else {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
}
}
If (0! 16)=(I %) {
16 k=16 - (I %);
for (j=0; JSprintf (binstr, "% s", binstr);
}
Sprintf (binstr, "% s", binstr);
K=16 - k;
For (j=I - k; JSprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
Printf (" % s \ n ", binstr);
}
}

CodePudding user response:

Receiving and sending to the===="" "

Receiving part
Char szTemp [200].
Int n=Receive (szTemp, 200);
SzTemp [n]=0;

CStringA szBuf (szTemp);
Cstrings sTemp (szBuf);


Send some
The UpdateData ();
CStringA szSendBuf (m_sWords);
M_ClientSocket. Send (szSendBuf, szSendBuf GetLength ());//message
M_ListWords. AddString (_T (" send: ") + m_sWords);

CodePudding user response:

The sending part also use char * bai, just to guarantee the type consistent character set on both ends

CodePudding user response:

At the same time use WCHAR or CHAR
Use TCHAR, send length * sizeof (TCHAR)

CodePudding user response:

reference 5 floor zhao4zhong1 reply:
computer memory or file content or transport it is just a one-dimensional binary byte array and its corresponding binary address;
The human brain to a computer memory or file contents or transfer the content of the one-dimensional binary byte array and its corresponding binary address some parts as an integer, number of signed/unsigned number, floating point Numbers, complex Numbers, letters, digits, Chinese/Korean/French... Character/string, assembly instructions, functions, function parameters, heap, stack, arrays, Pointers, array pointer and pointer array, the array of arrays, pointer to pointer, two-dimensional arrays, character lattice, the coordinates of character strokes, black and white binary images and grayscale images, color images, audio, video, fingerprint information, id information...

For computer no noise, only the binary bytes; To the human brain is gibberish, GBK: 0 xb0 0 xa1, Unicode - 16 LE: 0 x4a 0 x55, Unicode - 16 BE: 0 x55 0 x4a, utf-8 8-0 x95 xE5 0 0 x8a

 void HexDump) (char * buf, int len, int addr) {
Int I, j, k;
Char binstr [80].

for (i=0; IIf (0==16) (I %) {
Sprintf (binstr, "% x - 08", I + addr);
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
} else if (15==16) (I %) {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
Sprintf (binstr, "% s", binstr);
For (j=I - 15; J<=I; J++) {
Sprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
Printf (" % s \ n ", binstr);
} else {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
}
}
If (0! 16)=(I %) {
16 k=16 - (I %);
for (j=0; JSprintf (binstr, "% s", binstr);
}
Sprintf (binstr, "% s", binstr);
K=16 - k;
For (j=I - k; JSprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
Printf (" % s \ n ", binstr);
}
}


For computer no noise, only the binary bytes; To the human brain is gibberish, miss zhao that makes sense,

CodePudding user response:

For computer no noise, only the binary bytes; To the human brain is gibberish, GBK: 0 xb0 0 xa1, Unicode - 16 LE: 0 x4a 0 x55, Unicode - 16 BE: 0 x55 0 x4a, utf-8 8-0 x8a x95 xE5 0 0

CodePudding user response:

With a char array to receive, then char [] to cstrings

CodePudding user response:

If the client and the server is written, all with the same coding formats,
If the service side live client is bill writing, use utf-8.
Conversion method is as follows:

Would be * UTF8ToUnicode (const char * STR)
{
Int textlen=0;
The result would be *;
Textlen=MultiByteToWideChar (CP_UTF8, 0, STR, 1, NULL, 0).
Result=(*) would be malloc sizeof ((textlen + 1) * (would));
Memset (result, 0, sizeof (textlen + 1) * (would));
MultiByteToWideChar (CP_UTF8, 0, STR, 1, (LPWSTR) result, textlen);
Return the result.
}

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related