0

In order to binary serialize a decimal type I do the following:

var ms = new MemoryStream();
decimal d = 123.45M;
int[] bits = Decimal.GetBits(d);
for(int i=0; i<4; i++)
    ms.Write(BitConverter.GetBytes(bits[i]),0,4);

Now that I have a int[] bits variable how can I convert that back to a decimal? Or how can I convert the 16 bytes in the memory stream back to a decimal? Is this possible to do without having to use unsafe code?

With float, int, bool and other types that is simple to do. I just use the System.Convert and BitConverter static classes.

Tono Nam
  • 34,064
  • 78
  • 298
  • 470

1 Answers1

0

Sorry for posting the question I should have had tried longer. I was confused because all the other types made use of System.Convert and BitConverter.

Anyways just have to do this:

decimal originalDecimal = 123.45M;
int[] bits = Decimal.GetBits(originalDecimal);
decimal cloneDecimal = new Decimal(bits);
Tono Nam
  • 34,064
  • 78
  • 298
  • 470