是否保证本机类型的二进制表示在所有目标上都相同?
我计划将我的数据以二进制格式存储为资源,将其读入 int 缓冲区,然后将其直接传递给本机 C++ 函数,该函数可能会将其转换为结构/类并使用它。没有指针显然,只是整数和浮点数。
问题是 - 我需要做什么样的修复?我想我需要检查 ByteOrder.nativeOrder() ,弄清楚它是大端还是大端。小尾数,并在需要时执行字节交换。
除此之外,浮点数大概可以保证采用 IEEE 754 格式?还有其他我完全忽略的警告吗?
(另外 - 因为我正在使用NDK,我知道它已经是什么架构(在我的例子中是 ARMv7-A),所以我可以在技术上跳过字节序恶作剧,只按原样获取数据吗?)
I"m planning to store my data in a binary format as a resource, read it into an int buffer and basically pass it straight down to a native C++ function, which might cast it to a struct/class and work with it. No pointers, obviously, just ints and floats.
The question is - what kind of fixing up do I need to do? I suppose that I need to check ByteOrder.nativeOrder()
, figure out if it's big endian or little endian, and perform byte-swapping if need be.
Other than that, floats are presumably guaranteed to be expected in IEEE 754 format? Are there any other caveats I'm completely overlooking here?
(Also - since I'm compiling using the NDK, I know what architecture it is already (ARMv7-A, in my case), so can I technically skip the endian shenanigans and just take the data the way it is?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ARM 支持大端和小端。这很可能是由操作系统设置的,因此可能值得提前检查一下。
还有结构体中字大小填充的问题:
sizeof
为 8,而不是预期的 5 个字节。这是为了使 int 字对齐。通常将所有内容对齐 4 个字节,并可能使用 gcc 打包属性 (struct my_packed_struct __attribute__ ((__packed__))
)也是如此。这将确保结构的内部结构符合您的预期。或者使用 Android Simulator 为您生成数据文件。
ARM support both big and little endian. This will most probably be set by the OS so it might be worth while checking this out beforehand.
There is also the issue of padding to word size in a struct:
will have a
sizeof
8 and not the expected 5 bytes. This is so that the int will be word aligned. Generally align everything to 4 bytes and probably use the gcc packed attribute (struct my_packed_struct __attribute__ ((__packed__))
) as well. This will ensure that the internals of the struct are as you expect.Alternatively use the Android Simulator to generate the data file for you.