是C#尾段敏感?

编程入门 行业动态 更新时间:2024-10-27 10:23:28
本文介绍了是C#尾段敏感?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是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#尾段敏感?

本文发布于:2023-05-25 00:26:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/218682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:尾段   敏感

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!