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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With