472,958 Members | 1,806 Online

# Convert VB Double to Pascal Real

Hi,

I'm writing a program using VB.NET that needs to communicate with a DOS
Pascal program than cannot be modified. The communication channel is through
some file databases, and I have a huge problem writing VB Double values to
the file so as the Pascal program can read them as Pascal Real values.

I've managed to find the algorithm to read the Pascal Real format and
convert it to a VB Double, but I cannot figure out the opposite algorithm.

Can someone help me reverse my algorithm and develop the function
"DoubleToReal (ByVal Data As Double) As String"

Here is the conversion from real to double:

Public Function RealToDouble(ByVal Data As String) As Double
Dim dMantissa As Double
Dim i As Integer
Dim j As Long
Dim k As Long

If Len(Data) <> 6 Then
'Err.Raise
'exception
Exit Function
End If

'accumulate the mantissa
dMantissa = 1
For i = 6 To 2 Step -1
For j = CType(IIf(i = 6, 6, 7), Long) To 0 Step -1
k = k + 1
If (Asc(Mid\$(Data, i, 1)) And CType(2 ^ j, Long)) <> 0 Then
dMantissa = dMantissa + 2 ^ -k
End If
Next j
Next i

'finally, assemble all the pieces into a number
If (Asc(Mid\$(Data, 6, 1)) And &H80) = &H80 Then
RealToDouble = -dMantissa * 2 ^ (Asc(Mid\$(Data, 1, 1)) - 129)
Else
RealToDouble = dMantissa * 2 ^ (Asc(Mid\$(Data, 1, 1)) - 129)
End If

Try
Return ([Decimal].Round(CDec(RealToDouble), 2))
Catch ex As Exception
'MsgBox("RealToDouble, Conversion error: " & Data & "; " &
RealToDouble.ToString, MsgBoxStyle.Critical)
Return 0
End Try

End Function

David
Nov 20 '05 #1
17 4286 I probably have not understood you correctly, but can you not simply convert
the double to a string an then truncate the string to the correct length?

As far as I am aware, a real number is simply a number which is not
imaginary, so I dont know how this differs in a Pascal Real number. Your
function seems to convert a string into a double but goes a long way around.
BTW, I actually tried to convert "1.2345" and it returned 0.0

I'm sure Im missing something here, can you illuminate ?

Regards - OHM

David Scemama wrote:
Hi,

I'm writing a program using VB.NET that needs to communicate with a
DOS Pascal program than cannot be modified. The communication channel
is through some file databases, and I have a huge problem writing VB
Double values to the file so as the Pascal program can read them as
Pascal Real values.

I've managed to find the algorithm to read the Pascal Real format and
convert it to a VB Double, but I cannot figure out the opposite
algorithm.

Can someone help me reverse my algorithm and develop the function
"DoubleToReal (ByVal Data As Double) As String"

Here is the conversion from real to double:

Public Function RealToDouble(ByVal Data As String) As Double
Dim dMantissa As Double
Dim i As Integer
Dim j As Long
Dim k As Long

If Len(Data) <> 6 Then
'Err.Raise
'exception
Exit Function
End If

'accumulate the mantissa
dMantissa = 1
For i = 6 To 2 Step -1
For j = CType(IIf(i = 6, 6, 7), Long) To 0 Step -1
k = k + 1
If (Asc(Mid\$(Data, i, 1)) And CType(2 ^ j, Long)) <>
0 Then dMantissa = dMantissa + 2 ^ -k
End If
Next j
Next i

'finally, assemble all the pieces into a number
If (Asc(Mid\$(Data, 6, 1)) And &H80) = &H80 Then
RealToDouble = -dMantissa * 2 ^ (Asc(Mid\$(Data, 1, 1)) -
129) Else
RealToDouble = dMantissa * 2 ^ (Asc(Mid\$(Data, 1, 1)) -
129) End If

Try
Return ([Decimal].Round(CDec(RealToDouble), 2))
Catch ex As Exception
'MsgBox("RealToDouble, Conversion error: " & Data & "; " &
RealToDouble.ToString, MsgBoxStyle.Critical)
Return 0
End Try

End Function

David

Nov 20 '05 #2
Hi OHM

Can you take a look at that answer from cc I have sended in and give some

All is there just paste it on a new form and run.

Or I don't understand it (and no user will understand it) or it is a real
big bug.

Cor
Nov 20 '05 #3
I think my reader must be out of sync here. I dont see this reply. I have
noticed this with quite a few posts lately. Can you repost under this one

Thanks - OHM

Cor wrote:
Hi OHM

Can you take a look at that answer from cc I have sended in and give

All is there just paste it on a new form and run.

Or I don't understand it (and no user will understand it) or it is a
real big bug.

Cor

Nov 20 '05 #4
Hi OHM this was a question from cc
how do I make keyboard & mouse temporary not response to desktop

I did want to answer with simple" me.enabled = false", but did not trust it
because this answer seems to simple to me. So I did test it, and I find
this strange and do not understand it.

Who will test it also and tell me if this is normal behaviour or a bug?
The problem is that when you pusth the button when enabled is false, it
keeps responding to the events, so you have to push several times on the not
locked button.

Framework 1.1

Cor

\\\
' needs one button and one label on a form
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Static i As Integer
Me.Button1.Text = "locked"
Dim ctr As Control
' Me.Enabled = false did not work so
'I did try this but does not work either
For Each ctr In Me.Controls
ctr.Enabled = False
Next
Me.Refresh()
Dim y As Integer
For y = 1 To 20
i = i + 50
Me.Label1.Text = i.ToString
Me.Refresh()
Next
For Each ctr In Me.Controls
ctr.Enabled = True
Next
Me.Button1.Text = "Unlocked"
End Sub
///

Nov 20 '05 #5
But this post is about Mouse and keyboard, the current thread we are in is
regarding Double/Pascal Real number conversions?

Whats happening, am I going mad ?

Regards OHM
Cor wrote:
Hi OHM this was a question from cc
how do I make keyboard & mouse temporary not response to desktop

I did want to answer with simple" me.enabled = false", but did not
trust it because this answer seems to simple to me. So I did test
it, and I find this strange and do not understand it.

Who will test it also and tell me if this is normal behaviour or a
bug?
The problem is that when you pusth the button when enabled is false,
it keeps responding to the events, so you have to push several times
on the not locked button.

Framework 1.1

Cor

\\\
' needs one button and one label on a form
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Static i As Integer
Me.Button1.Text = "locked"
Dim ctr As Control
' Me.Enabled = false did not work so
'I did try this but does not work either
For Each ctr In Me.Controls
ctr.Enabled = False
Next
Me.Refresh()
Dim y As Integer
For y = 1 To 20
i = i + 50
Me.Label1.Text = i.ToString
Me.Refresh()
Next
For Each ctr In Me.Controls
ctr.Enabled = True
Next
Me.Button1.Text = "Unlocked"
End Sub
///

Nov 20 '05 #6
Hi OHM

No I asket you to look to a posible Bug I had posted.

I was curious if I became crazy or it was a bug.

Cor
Nov 20 '05 #7
The Pascal Real type, is stored on 6 bytes with a very special coding. When
you write a real value in a file, 6 bytes are written to represent the value
(obviously, no language writes the string representation of the real !). My
function reads the 6 bytes and convert them to a VB double.

I need the reverse function !

David
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bp**********@titan.btinternet.com...
I probably have not understood you correctly, but can you not simply convert the double to a string an then truncate the string to the correct length?

As far as I am aware, a real number is simply a number which is not
imaginary, so I dont know how this differs in a Pascal Real number. Your
function seems to convert a string into a double but goes a long way around. BTW, I actually tried to convert "1.2345" and it returned 0.0

I'm sure Im missing something here, can you illuminate ?

Regards - OHM

David Scemama wrote:
Hi,

I'm writing a program using VB.NET that needs to communicate with a
DOS Pascal program than cannot be modified. The communication channel
is through some file databases, and I have a huge problem writing VB
Double values to the file so as the Pascal program can read them as
Pascal Real values.

I've managed to find the algorithm to read the Pascal Real format and
convert it to a VB Double, but I cannot figure out the opposite
algorithm.

Can someone help me reverse my algorithm and develop the function
"DoubleToReal (ByVal Data As Double) As String"

Here is the conversion from real to double:

Public Function RealToDouble(ByVal Data As String) As Double
Dim dMantissa As Double
Dim i As Integer
Dim j As Long
Dim k As Long

If Len(Data) <> 6 Then
'Err.Raise
'exception
Exit Function
End If

'accumulate the mantissa
dMantissa = 1
For i = 6 To 2 Step -1
For j = CType(IIf(i = 6, 6, 7), Long) To 0 Step -1
k = k + 1
If (Asc(Mid\$(Data, i, 1)) And CType(2 ^ j, Long)) <>
0 Then dMantissa = dMantissa + 2 ^ -k
End If
Next j
Next i

'finally, assemble all the pieces into a number
If (Asc(Mid\$(Data, 6, 1)) And &H80) = &H80 Then
RealToDouble = -dMantissa * 2 ^ (Asc(Mid\$(Data, 1, 1)) -
129) Else
RealToDouble = dMantissa * 2 ^ (Asc(Mid\$(Data, 1, 1)) -
129) End If

Try
Return ([Decimal].Round(CDec(RealToDouble), 2))
Catch ex As Exception
'MsgBox("RealToDouble, Conversion error: " & Data & "; " &
RealToDouble.ToString, MsgBoxStyle.Critical)
Return 0
End Try

End Function

David

Nov 20 '05 #8
This may be because the message goes out in the message queue, the button
does not respond to it until the loop finishes, then it processes the click
( I think ).

Regards - OHM
Cor wrote:
Hi OHM this was a question from cc
how do I make keyboard & mouse temporary not response to desktop

I did want to answer with simple" me.enabled = false", but did not
trust it because this answer seems to simple to me. So I did test
it, and I find this strange and do not understand it.

Who will test it also and tell me if this is normal behaviour or a
bug?
The problem is that when you pusth the button when enabled is false,
it keeps responding to the events, so you have to push several times
on the not locked button.

Framework 1.1

Cor

\\\
' needs one button and one label on a form
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Static i As Integer
Me.Button1.Text = "locked"
Dim ctr As Control
' Me.Enabled = false did not work so
'I did try this but does not work either
For Each ctr In Me.Controls
ctr.Enabled = False
Next
Me.Refresh()
Dim y As Integer
For y = 1 To 20
i = i + 50
Me.Label1.Text = i.ToString
Me.Refresh()
Next
For Each ctr In Me.Controls
ctr.Enabled = True
Next
Me.Button1.Text = "Unlocked"
End Sub
///

Nov 20 '05 #9
But should it do that?
Nov 20 '05 #10
I dont know the answer to this. I suggest you post the following this
specific question at top level in the group:
Regards - OHM

Cor wrote:
But should it do that?

Nov 20 '05 #11
Hi David,

I think this is a much to dificult approach for a newsgroup.
Maybe if guys like Fergus Cooney where here now, he could help you because
he likes this.

But he is not and I don't know if he will return soon.

Every byte in a file is representent by a hex value.

Can you not represent the bytes in hex form representing a normal decimal

Probably not me, because mostly this is not my stuff, but you never know.

Cor
Nov 20 '05 #12
Hi OHM,

Putted it in the vb.language.controls group.

Almost the private group from Herfied, I am curious what happens.

:-))

Cor
Nov 20 '05 #13
The exact representation of the Pascal Real type is:
Sign Significand Exponent
Width (bits) 1 39 8

David

"David Scemama" <da***********@nospam.wanadoo.fr> wrote in message
news:eF**************@TK2MSFTNGP12.phx.gbl...
The Pascal Real type, is stored on 6 bytes with a very special coding. When you write a real value in a file, 6 bytes are written to represent the value (obviously, no language writes the string representation of the real !). My function reads the 6 bytes and convert them to a VB double.

I need the reverse function !

David
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bp**********@titan.btinternet.com...
I probably have not understood you correctly, but can you not simply

convert
the double to a string an then truncate the string to the correct length?
As far as I am aware, a real number is simply a number which is not
imaginary, so I dont know how this differs in a Pascal Real number. Your
function seems to convert a string into a double but goes a long way

around.
BTW, I actually tried to convert "1.2345" and it returned 0.0

I'm sure Im missing something here, can you illuminate ?

Regards - OHM

David Scemama wrote:
Hi,

I'm writing a program using VB.NET that needs to communicate with a
DOS Pascal program than cannot be modified. The communication channel
is through some file databases, and I have a huge problem writing VB
Double values to the file so as the Pascal program can read them as
Pascal Real values.

I've managed to find the algorithm to read the Pascal Real format and
convert it to a VB Double, but I cannot figure out the opposite
algorithm.

Can someone help me reverse my algorithm and develop the function
"DoubleToReal (ByVal Data As Double) As String"

Here is the conversion from real to double:

Public Function RealToDouble(ByVal Data As String) As Double
Dim dMantissa As Double
Dim i As Integer
Dim j As Long
Dim k As Long

If Len(Data) <> 6 Then
'Err.Raise
'exception
Exit Function
End If

'accumulate the mantissa
dMantissa = 1
For i = 6 To 2 Step -1
For j = CType(IIf(i = 6, 6, 7), Long) To 0 Step -1
k = k + 1
If (Asc(Mid\$(Data, i, 1)) And CType(2 ^ j, Long)) <>
0 Then dMantissa = dMantissa + 2 ^ -k
End If
Next j
Next i

'finally, assemble all the pieces into a number
If (Asc(Mid\$(Data, 6, 1)) And &H80) = &H80 Then
RealToDouble = -dMantissa * 2 ^ (Asc(Mid\$(Data, 1, 1)) -
129) Else
RealToDouble = dMantissa * 2 ^ (Asc(Mid\$(Data, 1, 1)) -
129) End If

Try
Return ([Decimal].Round(CDec(RealToDouble), 2))
Catch ex As Exception
'MsgBox("RealToDouble, Conversion error: " & Data & "; " &
RealToDouble.ToString, MsgBoxStyle.Critical)
Return 0
End Try

End Function

David

Nov 20 '05 #14
You'll probably get the reply in German !.

Regards - OHM

Cor wrote:
Hi OHM,

Putted it in the vb.language.controls group.

Almost the private group from Herfied, I am curious what happens.

:-))

Cor

Nov 20 '05 #15
Hay David,

This is clearly a complex conversion. I found this on the internet. It is
Quick Basic rendering of a solution to your problem ( Hopefully ). You will
need to amend it a bit for VB.NET, but hopefully it gives you a template to
work with.

HTH - OHM
DECLARE FUNCTION power# (x!, y AS INTEGER)
DECLARE SUB RealConv (RealCost AS ANY, NewCost#)
' the QBASIC equivalent of the above Pascal struct:
TYPE PASdataRecord
EmpNameLength AS STRING * 1
EmpName AS STRING * 30
Number AS INTEGER
Wage AS STRING * 6
END TYPE
' set up the file to be opened and read from QBASIC
DIM EmployeeDAT AS PASdataRecord
OPEN "EMPLOY.DAT" FOR RANDOM ACCESS READ LOCK WRITE AS #1 LEN =
LEN(EmployeeDAT)
' read the file a record at a time until the end
DO WHILE NOT EOF(1)
CLS
Count = Count + 1
SEEK #1, Count
GET #1, , EmployeeDAT
' strip out the actual string using the first length byte
EmployeeDAT.EmpName = MID\$(EmployeeDAT.EmpName, 1,
ASC(EmployeeDAT.EmpNameLength))
' the following routine converts the pascal real to Qbasic double
CALL RealConv(EmployeeDAT.Wage, BASwage#)
PRINT "Employee number = "; EmployeeDAT.Number
PRINT "Employee EmpName = "; EmployeeDAT.EmpName
PRINT "Employee wage = "; BASwage
LOOP
FUNCTION power# (x, y AS INTEGER)
' simple x to the power of y function
power# = EXP(y * LOG(x))
END FUNCTION

SUB RealConv (Real\$, NewCost#)
' create an array to hold each byte of the real string
DIM RealHold(6)
RealHold(1) = ASC(MID\$(Real\$, 1, 1))
RealHold(2) = ASC(MID\$(Real\$, 2, 1))
RealHold(3) = ASC(MID\$(Real\$, 3, 1))
RealHold(4) = ASC(MID\$(Real\$, 4, 1))
RealHold(5) = ASC(MID\$(Real\$, 5, 1))
RealHold(6) = ASC(MID\$(Real\$, 6, 1))
' if positive contains a number then its negative
positive = &H80 AND RealHold(6)
' clear the Pos/Neg bit from byte 6
RealHold(6) = &H80 OR RealHold(6)
' set up the significand as 1.0
Significand# = 1#
' check each individual bit for on/off; if on then multiply out the
' number (2,4,8,16,32,64,128, etc.)
FOR bytecheck = 2 TO 6
' bit 0 of byte
IF (RealHold(bytecheck) AND &H1) = 1 THEN
Significand# = Significand# + power(2, (0 + (bytecheck - 2) * 8))
END IF
' bit 1 of byte
IF (RealHold(bytecheck) AND &H2) = 2 THEN
Significand# = Significand# + power(2, (1 + (bytecheck - 2) * 8))
END IF
' bit 2 of byte
IF (RealHold(bytecheck) AND &H4) = 4 THEN
Significand# = Significand# + power(2, (2 + (bytecheck - 2) * 8))
END IF
' bit 3 of byte
IF (RealHold(bytecheck) AND &H8) = 8 THEN
Significand# = Significand# + power(2, (3 + (bytecheck - 2) * 8))
END IF
' bit 4 of byte
IF (RealHold(bytecheck) AND &H10) = 16 THEN
Significand# = Significand# + power(2, (4 + (bytecheck - 2) * 8))
END IF
' bit 5 of byte
IF (RealHold(bytecheck) AND &H20) = 32 THEN
Significand# = Significand# + power(2, (5 + (bytecheck - 2) * 8))
END IF
' bit 6 of byte
IF (RealHold(bytecheck) AND &H40) = 64 THEN
Significand# = Significand# + power(2, (6 + (bytecheck - 2) * 8))
END IF
' bit 7 of byte
IF (RealHold(bytecheck) AND &H80) = 128 THEN
Significand# = Significand# + power(2, (7 + (bytecheck - 2) * 8))
END IF
NEXT
' normalize the number by dividing calculated number by a number with all
' bits turned on: 2 to the power of 40
Significand# = Significand# / power(2, 40)
' calculate in the exponent
Number# = Significand# * power(2, (RealHold(1) - 128))
' set the pos/neg sign
IF positive > 0 THEN Number# = Number# * -1
NewCost# = Number#
END
Nov 20 '05 #16
Unfortunately, this is the exact contrary of what I would like to do. This
function converts the Pascal Real contained in a string to a double.

Thanks
David

"One Handed Man" <Bo****@Duck.net> wrote in message
news:bq**********@titan.btinternet.com...
Hay David,

This is clearly a complex conversion. I found this on the internet. It is
Quick Basic rendering of a solution to your problem ( Hopefully ). You will need to amend it a bit for VB.NET, but hopefully it gives you a template to work with.

HTH - OHM
DECLARE FUNCTION power# (x!, y AS INTEGER)
DECLARE SUB RealConv (RealCost AS ANY, NewCost#)
' the QBASIC equivalent of the above Pascal struct:
TYPE PASdataRecord
EmpNameLength AS STRING * 1
EmpName AS STRING * 30
Number AS INTEGER
Wage AS STRING * 6
END TYPE
' set up the file to be opened and read from QBASIC
DIM EmployeeDAT AS PASdataRecord
OPEN "EMPLOY.DAT" FOR RANDOM ACCESS READ LOCK WRITE AS #1 LEN =
LEN(EmployeeDAT)
' read the file a record at a time until the end
DO WHILE NOT EOF(1)
CLS
Count = Count + 1
SEEK #1, Count
GET #1, , EmployeeDAT
' strip out the actual string using the first length byte
EmployeeDAT.EmpName = MID\$(EmployeeDAT.EmpName, 1,
ASC(EmployeeDAT.EmpNameLength))
' the following routine converts the pascal real to Qbasic double
CALL RealConv(EmployeeDAT.Wage, BASwage#)
PRINT "Employee number = "; EmployeeDAT.Number
PRINT "Employee EmpName = "; EmployeeDAT.EmpName
PRINT "Employee wage = "; BASwage
LOOP
FUNCTION power# (x, y AS INTEGER)
' simple x to the power of y function
power# = EXP(y * LOG(x))
END FUNCTION

SUB RealConv (Real\$, NewCost#)
' create an array to hold each byte of the real string
DIM RealHold(6)
RealHold(1) = ASC(MID\$(Real\$, 1, 1))
RealHold(2) = ASC(MID\$(Real\$, 2, 1))
RealHold(3) = ASC(MID\$(Real\$, 3, 1))
RealHold(4) = ASC(MID\$(Real\$, 4, 1))
RealHold(5) = ASC(MID\$(Real\$, 5, 1))
RealHold(6) = ASC(MID\$(Real\$, 6, 1))
' if positive contains a number then its negative
positive = &H80 AND RealHold(6)
' clear the Pos/Neg bit from byte 6
RealHold(6) = &H80 OR RealHold(6)
' set up the significand as 1.0
Significand# = 1#
' check each individual bit for on/off; if on then multiply out the
' number (2,4,8,16,32,64,128, etc.)
FOR bytecheck = 2 TO 6
' bit 0 of byte
IF (RealHold(bytecheck) AND &H1) = 1 THEN
Significand# = Significand# + power(2, (0 + (bytecheck - 2) * 8))
END IF
' bit 1 of byte
IF (RealHold(bytecheck) AND &H2) = 2 THEN
Significand# = Significand# + power(2, (1 + (bytecheck - 2) * 8))
END IF
' bit 2 of byte
IF (RealHold(bytecheck) AND &H4) = 4 THEN
Significand# = Significand# + power(2, (2 + (bytecheck - 2) * 8))
END IF
' bit 3 of byte
IF (RealHold(bytecheck) AND &H8) = 8 THEN
Significand# = Significand# + power(2, (3 + (bytecheck - 2) * 8))
END IF
' bit 4 of byte
IF (RealHold(bytecheck) AND &H10) = 16 THEN
Significand# = Significand# + power(2, (4 + (bytecheck - 2) * 8))
END IF
' bit 5 of byte
IF (RealHold(bytecheck) AND &H20) = 32 THEN
Significand# = Significand# + power(2, (5 + (bytecheck - 2) * 8))
END IF
' bit 6 of byte
IF (RealHold(bytecheck) AND &H40) = 64 THEN
Significand# = Significand# + power(2, (6 + (bytecheck - 2) * 8))
END IF
' bit 7 of byte
IF (RealHold(bytecheck) AND &H80) = 128 THEN
Significand# = Significand# + power(2, (7 + (bytecheck - 2) * 8))
END IF
NEXT
' normalize the number by dividing calculated number by a number with all
' bits turned on: 2 to the power of 40
Significand# = Significand# / power(2, 40)
' calculate in the exponent
Number# = Significand# * power(2, (RealHold(1) - 128))
' set the pos/neg sign
IF positive > 0 THEN Number# = Number# * -1
NewCost# = Number#
END

Nov 20 '05 #17
The exact representation of the Pascal Real type is:
Sign Significand Exponent
Width (bits) 1 39 8

David

"David Scemama" <da***********@nospam.wanadoo.fr> wrote in message
news:eF**************@TK2MSFTNGP12.phx.gbl...
The Pascal Real type, is stored on 6 bytes with a very special coding. When you write a real value in a file, 6 bytes are written to represent the value (obviously, no language writes the string representation of the real !). My function reads the 6 bytes and convert them to a VB double.

I need the reverse function !

David
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bp**********@titan.btinternet.com...
I probably have not understood you correctly, but can you not simply

convert
the double to a string an then truncate the string to the correct length?
As far as I am aware, a real number is simply a number which is not
imaginary, so I dont know how this differs in a Pascal Real number. Your
function seems to convert a string into a double but goes a long way

around.
BTW, I actually tried to convert "1.2345" and it returned 0.0

I'm sure Im missing something here, can you illuminate ?

Regards - OHM

David Scemama wrote:
Hi,

I'm writing a program using VB.NET that needs to communicate with a
DOS Pascal program than cannot be modified. The communication channel
is through some file databases, and I have a huge problem writing VB
Double values to the file so as the Pascal program can read them as
Pascal Real values.

I've managed to find the algorithm to read the Pascal Real format and
convert it to a VB Double, but I cannot figure out the opposite
algorithm.

Can someone help me reverse my algorithm and develop the function
"DoubleToReal (ByVal Data As Double) As String"

Here is the conversion from real to double:

Public Function RealToDouble(ByVal Data As String) As Double
Dim dMantissa As Double
Dim i As Integer
Dim j As Long
Dim k As Long

If Len(Data) <> 6 Then
'Err.Raise
'exception
Exit Function
End If

'accumulate the mantissa
dMantissa = 1
For i = 6 To 2 Step -1
For j = CType(IIf(i = 6, 6, 7), Long) To 0 Step -1
k = k + 1
If (Asc(Mid\$(Data, i, 1)) And CType(2 ^ j, Long)) <>
0 Then dMantissa = dMantissa + 2 ^ -k
End If
Next j
Next i

'finally, assemble all the pieces into a number
If (Asc(Mid\$(Data, 6, 1)) And &H80) = &H80 Then
RealToDouble = -dMantissa * 2 ^ (Asc(Mid\$(Data, 1, 1)) -
129) Else
RealToDouble = dMantissa * 2 ^ (Asc(Mid\$(Data, 1, 1)) -
129) End If

Try
Return ([Decimal].Round(CDec(RealToDouble), 2))
Catch ex As Exception
'MsgBox("RealToDouble, Conversion error: " & Data & "; " &
RealToDouble.ToString, MsgBoxStyle.Critical)
Return 0
End Try

End Function

David

Nov 20 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 17 by: Pascal | last post by: Hello, I've a binary file with data in it. This file come from an old ms dos application (multilog ~ 1980). In this application, a field is declared as a 'decimal' (999 999 999.99). I put 0.00... 5 by: Al | last post by: I am about to start rewriting a fortran program to ansi C++. Would it be best to convert the variables that are 'real' to float? double? long double? 12 by: Alan | last post by: how to convert double to short ? for example, I want to convert double doubleVal1 = 15000.1; double doubleVal2 = 12000.0; short shortVal; shortVal = doubleVal1 - doubleVal2; I... 28 by: Skybuck Flying | last post by: Hi, I think I understand now a bit better what the difference is between a c compiler and a pascal compiler. For example: When compiling source code with a pascal compiler. The pascal... 2 by: Pascal | last post by: Why does this not work, and how should i do this convert in stead: string x = double.MinValue.ToString(); double y = Convert.ToDouble(x); i get this exception: An unhandled exception of type... 2 by: Piterek | last post by: Hello. I have a question, did someone can convert from VB.NET source code to Delphi7 code? Dim n, x As Double Dim tab() As Double = {-3, 0.5, -0.05}... 5 by: karmalax | last post by: public static string Somme(string T) { string text1 = ""; double num5 = Strings.Len(T) + 1; int num3 = 7; 3 by: mrajanikrishna | last post by: Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; ... 54 by: Ruud | last post by: Hallo allemaal, During the conversion of my program from Pascal to C, I was more or less able to find the C equivalent of most Pascal functions so far. Only four gave me some real trouble. I... 0 by: Aliciasmith | last post by: In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app... 0 by: tracyyun | last post by: Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to... 2 by: giovanniandrean | last post by: The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions... 4 by: NeoPa | last post by: Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :... 3 by: NeoPa | last post by: Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all... 1 by: Teri B | last post by: Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and... 0 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on... 0 by: NeoPa | last post by: Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it... 0 by: isladogs | last post by: The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...