버퍼에 빅엔디안으로 데이터가 있고 이것을 변수에 담아야 할때 아래 코드를 사용하면 된다.
private uint ConvertBigEndianUInt(byte[] buffer, int offset)
{
uint ret = (uint)(buffer[offset] << 24);
ret |= (uint)(buffer[offset + 1] << 16);
ret |= (uint)(buffer[offset + 2] << 8);
ret |= (uint)(buffer[offset + 3]);
return ret;
}
private ushort ConvertBigEndianUShort(byte[] buffer, int offset)
{
ushort ret = (ushort)(buffer[offset] << 8);
ret |= (ushort)(buffer[offset + 1]);
return ret;
}
private long ConvertBigEndianLong(byte[] buffer, int offset)
{
long ret = (long)(buffer[offset] << 56);
ret |= (long)(buffer[offset + 1] << 48);
ret |= (long)(buffer[offset + 2] << 40);
ret |= (long)(buffer[offset + 3] << 32);
ret |= (long)(buffer[offset + 4] << 24);
ret |= (long)(buffer[offset + 5] << 16);
ret |= (long)(buffer[offset + 6] << 8);
ret |= (long)(buffer[offset + 7]);
return ret;
}
댓글 없음:
댓글 쓰기