当前位置: 动力学知识库 > 问答 > 编程问答 >

delphi - Integer 5 equal to 35 in socket communication

问题描述:

I'm exchanging some data over socket, and at some point of the communication, I need to send a message like this:

SendBuff(#$06 + #$05 + Login, Length(Login) + 2);

Exploding the message:

"#$06" = header, I need to send 06 in the front of this message

"#$05" = length for Login, this is the problem in fact!!

"Login" = the AnsiString containing the login

"Length(Login) + 2" = the length of the buffer to be sent, is the length of the login + 2...

Ok, as I said, the problem is in the #$05, if I write this way, it works, the server recognize the message and authorize (considering the length of login is 5). But if I try this way:

SendBuff(#$06 + IntToStr(Length(Login)) + Login, Length(Login) + 2);

For some reason in fact the socket send the value '35'!!! I tried this:

SendBuff(#$06 + '5' + Login, Length(Login) + 2);

And same problem, the socket sent the value '35'... What's happening here guys? The SendBuff procedure is receiving parameters this way:

procedure SendBuff(InputBuff: AnsiString; PacketSize: Integer);

网友答案:

That is because the ASCII ordinal for the character 5 is 35. When you write:

IntToStr(Length(Login))

you are converting the integer with value 5 to a string. Which is '5'. And ord('5') is 35.

You should use:

AnsiChar(Length(Login))

and hope that Login is never more than 255 characters in length.

分享给朋友:
您可能感兴趣的文章:
随机阅读: