Home > Software engineering >  Socket programming BYTE and Chinese characters when converting the code
Socket programming BYTE and Chinese characters when converting the code

Time:09-20

First post code, the sender is as follows:
Cstrings str1="code";
BYTE cSendBuf [1024].
Memset (cSendBuf, 0, sizeof (cSendBuf));
CSendBuf [0]=13;
CSendBuf [1]=0 x05;
Memcpy (cSendBuf + 4, str1. GetBuffer (str1. GetLength ()), str1. GetLength ());//put cstrings in byte array

I want to send word is "code", in the last line to set breakpoints, and found two word corresponding to the four are "177? , 224? , 194? , 235?" Also received the same characters, and the receiving end receiver code below
BYTE MidBuf [1024].
Memset (MidBuf, 0, sizeof (MidBuf));

Memcpy (MidBuf cBuf, 128);//
Cstrings str222;
Str222. The Format (" % d ", MidBuf);
M_strAuxActivaInfo [0]=str222;
When converting midbuf str222 became a string of nine digit number
For bosses to help look at what is problem

CodePudding user response:

Str222. The Format (" "% d, MidBuf);
% s?

CodePudding user response:

May be is not the same as the sender and the receiver compilation mode? As he is UNICODE is multi-byte?
Instead the unified format, or try to UTF8

CodePudding user response:

Replaced with % s displayed after a into a small square,,,, and both sides are more bytes

CodePudding user response:

note to send and receive data length!

CodePudding user response:

After all the data length to 1024 or display box

CodePudding user response:

The Chinese first into a byte array, and then send, such as utf8 encoding, decoding again after receiving, etc

CodePudding user response:

When I send to!
Memcpy (cSendBuf + 4, str1. GetBuffer (str1. GetLength ()), str1. GetLength ());//put cstrings in byte array

CodePudding user response:

"177 xb1=0, 224=0 xe0-0xfc, 194=0 xc2, 235=0 xeb"="code" (multibyte)

CodePudding user response:

Size problem?

CodePudding user response:

Specific to how to solve? Into the midbuf content are right, but into a string is wrong

CodePudding user response:

And the locations of the received data in buf and content are exactly the same

CodePudding user response:

Str222. The Format (" % s ", MidBuf);
AfxMessageBox (str222);

CodePudding user response:

Message box display is also a box

CodePudding user response:

1 the pure ASC code first see if there is something wrong?
2 choose the font support Chinese?
3 control panel, regional language option to see whether a non-unicode program language is Chinese?

CodePudding user response:

Memcpy (MidBuf cBuf, 128);//
What is in the cBuf?

CodePudding user response:

Just checked all of them are Chinese
Are the two words in the cbuf

CodePudding user response:

It is not
Cstrings str222;
MidBuf str222=(char *);
How str222 will you?

CodePudding user response:

Str222. The Format (" % d ", MidBuf); Changed to % s can be displayed,

In front of you that shows squares because you send the string two
CSendBuf [0]=13;
CSendBuf [1]=0 x05;

CodePudding user response:

Memcpy (cSendBuf + 4, str1. GetBuffer (str1. GetLength ()), str1. GetLength ()); Add 4 here, actually sent eight bytes in the past

CodePudding user response:

S also not line,,, + 4 just change the position of the data,

CodePudding user response:

Memcpy (MidBuf cBuf, 128); Copy here, in front of you skip the 4 bytes?

CodePudding user response:

Upstairs, the great god, incredibly can understand your code, really is designed

CodePudding user response:

Memcpy (MidBuf cBuf, 128); Copy here, in front of you skip the 4 bytes?
Upstairs to ask

CodePudding user response:

For each byte of the buffer function assist debugging:
 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:

Str222. The Format (" % d ", MidBuf); ?? Do you want to do? Will MidBuf as an int type to explain?

CodePudding user response:

The sender and the receiver encoding is consistent,
  • Related