本文介绍了如何阅读内存流中的短片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我尝试读取十六进制的2字节标题,如
Hi im try to read 2 bytes header in hex like this
52 01
但
but
MemoryStream
只允许使用ReadByte()。
这是我的代码
just allow ReadByte() only.
this is my code
ushort size = BitConverter.ToUInt16(new byte[] { (byte)ReceiveStream.ReadByte(), (byte)ReceiveStream.ReadByte() }, 0);
ReceiveStream.Position -= 2;
while (size > 0 && size <= (ReceiveStream.Length - ReceiveStream.Position))
{
//Log.Debug("Read: " + size + "-byte packet.");
// Read Packet Data including length
byte[] data = new byte[size];
ReceiveStream.Read(data, 0, size);
// Process packet.
MemoryStream stream = new MemoryStream(data, 2, data.Length - 2, false);
int opcode = stream.ReadByte();//TODO i want to read 2 bytes
//other code call the opcode value
}
这是我的问题:
this is my problem:
int opcode = stream.ReadByte();
操作码值必须是152,就像我给出的标题值一样。但是我的代码只读了52。
我需要一些帮助xD
我尝试过:
the opcode value must be "152" like the header value i've give. But my code just read "52" only.
I need some help xD
What I have tried:
ushort size = BitConverter.ToUInt16(new byte[] { (byte)ReceiveStream.ReadByte(), (byte)ReceiveStream.ReadByte() }, 0);
ReceiveStream.Position -= 2;
while (size > 0 && size <= (ReceiveStream.Length - ReceiveStream.Position))
{
//Log.Debug("Read: " + size + "-byte packet.");
// Read Packet Data including length
byte[] data = new byte[size];
ReceiveStream.Read(data, 0, size);
// Process packet.
MemoryStream stream = new MemoryStream(data, 2, data.Length - 2, false);
int opcode = stream.ReadByte();//TODO i want to read 2 bytes
//other code call the opcode value
}
推荐答案
byte a = 52;
byte b = 1;
ushort result = (ushort)((int) a | (b << 8));
这篇关于如何阅读内存流中的短片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!