是C#不断尾段敏感,例如,将如下代码:
Is C# ever Endian sensitive, for example, will code such as this:
int a = 1234567; short b = *(short*)&i;
始终分配相同的值到b。如果是这样,这将是什么样的价值?
always assign the same value to b. If so, what value will it be?
如果不是,有什么好方法是有处理字节排列顺序如果与指针的代码?
If not, what good ways are there to deal with endianness if code with pointers in?
推荐答案C#没有定义的字节顺序。在现实中,是的,它可能永远是小端(IIRC甚至在IA64,但我没有检查),但你最好应该检查 BitConverter.IsLittleEndian 字节顺序,如果是重要 - 或者只是使用位移等,而不是直接内存访问
C# doesn't define the endianness. In reality, yes it will probably always be little-endian (IIRC even on IA64, but I haven't checked), but you should ideally check BitConverter.IsLittleEndian if endianness is important - or just use bit-shifting etc rather than direct memory access.
引述protobuf网(构建尚未提交)几行:
To quote a few lines from protobuf-net (a build not yet committed):
WriteInt64(*(long*)&value); if (!BitConverter.IsLittleEndian) { // not fully tested, but this *should* work Reverse(ioBuffer, ioIndex - 8, 8); }
即。它会检查字节顺序,如果需要做一个翻转。
i.e. it checks the endianness and does a flip if necessary.
更多推荐
是C#尾段敏感?
发布评论