Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa Decimals and Protocol Buffers, compatibility with .NET

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.

like image 522
hocker Avatar asked Nov 15 '22 02:11

hocker


1 Answers

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.

like image 169
Marc Gravell Avatar answered Jan 03 '23 12:01

Marc Gravell