Cocoa 小数和协议缓冲区,与 .NET 兼容

发布于 2024-10-19 10:03:47 字数 687 浏览 1 评论 0原文

我一直在使用 CocoaTouch 编写的 iOS 应用程序中使用 Protocol Buffers,并且遇到了可能相当于十进制数字的菜鸟错误。

Protocol Buffers 没有原生十进制类型 - 请参阅 http://code .google.com/apis/protocolbuffers/docs/proto.html#scalar 适用于所有类型。相反,我做出了(现在显然是错误的)假设:浮点数可以代替小数。事实并非如此,因为存在舍入问题。

Cocoa 在 NSDecimal.h 中有一组 C 函数,看起来很有吸引力。然而,由于我正在与使用 .NET 编写的代码的服务器进行通信,因此我需要一种方法来转换两个系统都可以理解的格式。

我假设我会使用原始“字节”类型在两个系统之间传输数据,并在每个环境中转换为本机十进制类型:Cocoa 端的 NSDecimal,.NET 端的 Decimal。正是在这里,我不清楚如何继续。

我应该使用什么中间格式 - 二进制编码的十进制似乎是一种可能性,但我必须自己实现一种算法才能在两个系统上进行转换。

是否有一种我可以使用 .NET 和 Cocoa 都支持的字节编码十进制格式?

非常感谢。

I've been working with Protocol Buffers with an iOS app written using CocoaTouch and have run into what probably amounts to a rookie mistake with decimal numbers.

Protocol Buffers does not have a native decimal type - see http://code.google.com/apis/protocolbuffers/docs/proto.html#scalar for all the types. Instead I made the (now obviously incorrect) assumption that a floating point number would work in place of a decimal. It does not, as there are rounding problems.

Cocoa has a set of C functions in NSDecimal.h which look appealing to use instead. However since I'm communicating with a server which has code written in .NET, I am going to need a way to convert to and from a format that both systems can understand.

I assume I would use the raw "bytes" type to transfer the data between the two systems and convert to the native decimal type in each environment: NSDecimal on the Cocoa side, Decimal on the .NET side. It is here that I am unclear on how to proceed.

What intermediate format should I use - binary coded decimals seem like one possibility but I would have to implement an algorithm myself to convert on both systems.

Is there one byte-coded decimal format that both .NET and Cocoa speak that I could use?

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半城柳色半声笛 2024-10-26 10:03:47

随机为 protobuf-net 拼凑了一个相当任意的格式,它映射到 .NET decimal 类型 - 布局 在这里。这对于 .NET 到 .NET 很有效,但我对 Cocoa 小数了解不够,不知道它是如何适合的。

I randomly hacked together a fairly arbitrary format for protobuf-net, that maps to the .NET decimal type - the layout is here. This works well for .NET-to-.NET, but I do't know enough about Cocoa decimals to know how that fits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文