[英]C# How to represent an Int as four-byte integer
I'm trying to send the Length
of a Byte array to my server, so that it knows how much data to read. 我正在尝试将字节数组的Length
发送到我的服务器,以便它知道要读取多少数据。
I get the length of the Byte[] message
array using int messageLength = message.Length
我使用int messageLength = message.Length
获得了Byte[] message
数组的长度int messageLength = message.Length
How do I represent this integer messageLength
a four-byte integer? 如何表示此整数messageLength
为四字节整数?
使用BitConvertor BitConverter.GetBytes(message.Length);
You can use 您可以使用
int length = message.Length;
BitConvert.GetBytes(length);
http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx http://msdn.microsoft.com/zh-CN/library/system.bitconverter.aspx
message[0] = length & 0xFF;
message[1] = (length >> 8) & 0xFF;
message[2] = (length >> 16) & 0xFF;
message[3] = (length >> 24) & 0xFF;
To recover it... 要恢复它...
int length = message[0] | (message[1] << 8) | (message[2] << 16) | (message[3] << 24);
C#中的一个字节是8位。8位* 4字节= 32位..因此,您想使用System.Int32。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.