Cocoa Decimal and protocol buffers, .NET compatible.

I have been working with protocol buffers with an iOS application written using CocoaTouch and have come across what is likely to be a rookie decimal error.

Protocol buffers do not have a native decimal type - see http://code.google.com/apis/protocolbuffers/docs/proto.html#scalar for all types. Instead, I made the (now clearly incorrect) assumption that a floating point number would work instead of a decimal. This is not so because there are rounding issues.

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

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

Which intermediate format should I use - binary code decimal places seem to be one of the possibilities, but I will have to implement the algorithm itself to convert to both systems.

Is there a single byte-encoded decimal format that both .NET and Cocoa say I could use?

Thank you very much.

+4
source share
1 answer

I accidentally hacked into a pretty arbitrary format for protobuf-net that maps to the .NET decimal type - the layout is here , This works fine for .NET-to-.NET, but I don’t know enough about Cocoa decimal places to know how it works .

+1
source

Source: https://habr.com/ru/post/1341124/


All Articles