Home > Enterprise >  How should a System.Decimal be converted to BigEndian format for sending to a bigendian server?
How should a System.Decimal be converted to BigEndian format for sending to a bigendian server?

Time:10-30

IIUC the System.Decimal representation is an array of 32bit words in this order:

lo mid hi flags

From a little endian representation, to serialise for a System.Decimal recipient on a big endian server, is it necessary to:

  1. Preserve lo mid and hi order
  2. Reverse the bytes in each lo/mid/hi integer
  3. Preserve the flags integer byte ordering

I guess this means using GetBits to get the words , reverse each integer apart from the 4th element, and re-assemble the bytes to send to the server?

CodePudding user response:

The Decimal(int[]) constructor is documented as expecting the integers in a specific order,

bits is a four-element long array of 32-bit signed integers.

bits [0], bits [1], and bits [2] contain the low, middle, and high 32 bits of the 96-bit integer number.

bits [3] contains the scale factor and sign

which wouldn't change on an BigEndian platform.

If the Decimal is layed out in memory differently, that's handled internally.

Of course you may need to convert the Int32's to BigEndian if you're not using a standard framework like protocol buffers or json.

  • Related