Decimal64 data decoding

I am looking for an easy way to decode data stored in Decimal64 format (described here: http://en.wikipedia.org/wiki/Decimal64_floating-point_format ) using C #.

Any thoughts?

+4
source share
2 answers

Looked everywhere, finally, we implemented it ourselves.

Update Some of you asked us for a code - here is our code for it, we call it Float Decimal, I think it matches what Decimal 64 does, but without guarantees - please check it yourself.

Also note that the value of _size must be 8.

if (bytes[0] == 0) return 0; var s = ""; for (var i = 1; i < bytes.Length; i++) s += bytes[i].ToString("X").PadLeft(2, '0'); return decimal.Parse("." + s.TrimEnd('0')) * (decimal)Math.Pow(10 , ((bytes[0] & ~128) - 64)) * ((bytes[0] & 128) > 0 ? -1 : 1); 

To save:

  if (value != 0) { var negative = value < 0; var s = value.ToDecimal().ToString(CultureInfo.InvariantCulture).TrimStart('-', '0'); var i = s.IndexOf('.'); if (i >= 0) { s = s.Remove(i, 1); if (i == 0) { i = s.Length; s = s.TrimStart('0'); i = s.Length-i; } } else i = s.Length; bytes[0] = (byte)(64 + i + (negative ? 128 : 0)); s = s.PadRight((_size - 1) * 2, '0'); for (var j = 1; j < _size && (j - 1) * 2 < s.Length; j++) bytes[j] = byte.Parse(s.Substring((j - 1) * 2, 2), System.Globalization.NumberStyles.HexNumber); } 
+2
source

Read a little too fast.

I think you need to take a look at the BitConverter.ToSingle method in C #, but reorder the bytes to get the correct result. :)

BR jaggernauten

0
source

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


All Articles