几年前为了一个工作项目我不得不这么做。我不能发布这个,因为它是专有的,但TT字体的规范是
here
我还编写了一个名为BigEndianReader的实用程序类来处理从bigendian有序流中读取数据的操作,我很高兴将其放在这里,以使您的生活更轻松:
public class BigEndianReader
{
public static bool Read(Stream stm, out uint i)
{
int bhihi = stm.ReadByte();
if (bhihi == -1)
{
i = 0;
return false;
}
int bhi = stm.ReadByte();
if (bhi == -1)
{
i = 0;
return false;
}
int blo = stm.ReadByte();
if (blo == -1)
{
i = 0;
return false;
}
int blolo = stm.ReadByte();
if (blolo == -1)
{
i = 0;
return false;
}
i = (uint)((bhihi << 24) | (bhi << 16) | (blo << 8) | blolo);
return true;
}
public static bool Read(Stream stm, out int i)
{
int bhihi = stm.ReadByte();
if (bhihi == -1)
{
i = 0;
return false;
}
int bhi = stm.ReadByte();
if (bhi == -1)
{
i = 0;
return false;
}
int blo = stm.ReadByte();
if (blo == -1)
{
i = 0;
return false;
}
int blolo = stm.ReadByte();
if (blolo == -1)
{
i = 0;
return false;
}
i = ((bhihi << 24) | (bhi << 16) | (blo << 8) | blolo);
return true;
}
public static bool Read(Stream stm, out ushort s)
{
int bhi = stm.ReadByte();
if (bhi == -1)
{
s = 0;
return false;
}
int blo = stm.ReadByte();
if (blo == -1)
{
s = 0;
return false;
}
s = (ushort)(((bhi << 8) | blo) & 0xffff);
return true;
}
public static bool Read(Stream stm, out short s)
{
int bhi = stm.ReadByte();
if (bhi == -1)
{
s = 0;
return false;
}
int blo = stm.ReadByte();
if (blo == -1)
{
s = 0;
return false;
}
s = (short)(((bhi << 8) | blo) & 0xffff);
return true;
}
}
这不是特别优雅,但很容易使用:
int val;
if (!BigEndianReader.Read(stm, out val))
throw new SomeErrorOfSomeKind();
我想如果你足够关心的话,你可以把所有的扩展方法都放到流上,然后你可以读到:
int val;
if(!stm.BERead(out val)) // BE prefix for Big Endian
throw new SomeErrorOfSomeKind();