Jump to content
minheiro

Conversão dados floating point (6Bytes) em Double

Recommended Posts

minheiro

Boa tarde,

Utilizo um software cuja Base de Dados é um conjunto de ficheiros de acesso aleatorio 😡 .

Os dados tipo texto (strings) consigo ler e utilizar, os números se não estão como texto, são números com virgula flutuante com 6 Bytes.

Já consegui encontrar o formato em que os nºs são guardados mas não estou a conseguir converter:

Então são guardados com a seguinte estrutura (retiradop do site do produtor do emulador):

http://www.alphamicro.com/dss10/07300A05.pdf

"All numeric variables are assigned floating point format unless specified otherwise in the program. The

standard precision in use by the Alpha Micro system can be called "single-and-one-half," since it lies

midway between what are known as single precision and double precision formats.

The reason for this is that floating point numbers occupy six bytes of storage. Of the 48 bits in use for

each 6-byte variable, the high order bit is the sign of the mantissa.

The next 8 bits represent the signed exponent in excess-128 notation, giving a range of approximately

2.9*10^-39 through 1.7*10^38. The remaining 39 bits contain the mantissa, which is normalized with an

implied high-order bit of one. This gives an effective 40-bit mantissa which results in an accuracy of 11

significant digits."

Então temos:

SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM

em que:

S - Sinal (+/-)

E - Exponencial

M - Mantissa (parte do número de virgula flutuante.

http://www.pdcfaculty.org/rtureman/IntroCourses/CSC200/Projects/c200_ch01_excess_notation.htm

O double tem a seguinte estrutura:

  SEEEEEEE EEEEMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM

Já tenho o seguinte código:

Private Function RealToDouble(realBytes() As Byte) As Double

  ' bytes(7) bytes(6) bytes(5) bytes(4) bytes(3) bytes(2) bytes(1) bytes(0)

  '    SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM real

  ' SEEEEEEE EEEEMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM double

  Dim sign As Byte = realBytes(5) And &H80

  Dim exponent As Integer = (CInt(realBytes(5) << 1) Or CInt(realBytes(4) >> 7)) - 128 + 1024

  Dim doubleBytes(7) As Byte

  Buffer.BlockCopy(realBytes, 0, doubleBytes, 0, 5)

  doubleBytes(4) = doubleBytes(4) And Not &H80

  doubleBytes(7) = (sign) Or (exponent >> 4)

  doubleBytes(6) = (exponent << 4) And &HFF

  Return BitConverter.ToDouble(doubleBytes, 0)

End Function

(retirado de: http://social.msdn.microsoft.com/Forums/en/vblanguage/thread/dc474f6d-11af-4b25-b42f-c7145ced6e0b)

No entanto não estou a ter retorno dos valores correctos.

Conseguem ajudar?

Agradeço desde já,

:thumbsup::D:thumbsup:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.