### Static News Archive

(22-Jan-08 8:54) Converting from delphi 6 byte Real to c# double |

We have a legacy database that stores delphi 6 byte reals as a 2 byte short and a 4 byte long.
Converting this to a IEEE 754 64bit double such as that used in c# is a reasonably tricky exercise. The first thing to do is to understand the layout of the bits in these two different representations. The Delphi Real notation has the signed bit as the most significant, followed by 39 fraction bits, followed by the final 8 exponent bits. SFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF EEEEEEEE (48 bits long) However the IEEE 754 double notation has the fractional and exponential bits flipped. It also has 11 exponent bits, and 52 fractional bits: SEEEEEEE EEEEFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF (64 bits long) The code to do the conversion is reasonably straight forward, however there are a few things to look out for. 1) The exponent bias of the delphi real is 129, whereas the exponent bias of the IEEE 754 double is 1023. 2) Depending on how you want to shift the numbers around, be careful when dealing with signed and unsigned numbers. 3) Once you have the double bits in the right order, the magical BitConverter.Int64BitsToDouble() function will convert your bits to a double. Some helpful links to do the conversion (thanks to the creators of these sites!): http://babbage.cs.qc.edu/IEEE-754/Decimal.html http://en.wikipedia.org/wiki/Computer_numbering_formats#Why_binary.3F http://en.wikipedia.org/wiki/IEEE_floating-point_standard http://www.merlyn.demon.co.uk/pas-type.htm |

Post a comment |

<- (27-Dec-07 9:17) 3 Helpful blackberry... | (24-Jan-08 9:41) The relationship of ... -> |