title: Cocoa Decimals and Protocol Buffers, compatibility with .NET
body: <p>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. </p>
  
  <p>Protocol Buffers does not have a native decimal type - see <a href="http://code.google.com/apis/protocolbuffers/docs/proto.html#scalar" rel="noreferrer">http://code.google.com/apis/protocolbuffers/docs/proto.html#scalar</a> 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.</p>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>Is there one byte-coded decimal format that both .NET and Cocoa speak that I could use?</p>
  
  <p>Many thanks.</p>