Convert VB Double to Pascal Real

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Scemama

    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
    "DoubleToRe al (ByVal Data As Double) As String"

    Here is the conversion from real to double:

    Public Function RealToDouble(By Val 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(Rea lToDouble), 2))
    Catch ex As Exception
    'MsgBox("RealTo Double, Conversion error: " & Data & "; " &
    RealToDouble.To String, MsgBoxStyle.Cri tical)
    Return 0
    End Try

    End Function

    Thanks for your help
    David


  • One Handed Man

    #2
    Re: Convert VB Double to Pascal Real

    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:[color=blue]
    > 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
    > "DoubleToRe al (ByVal Data As Double) As String"
    >
    > Here is the conversion from real to double:
    >
    > Public Function RealToDouble(By Val 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(Rea lToDouble), 2))
    > Catch ex As Exception
    > 'MsgBox("RealTo Double, Conversion error: " & Data & "; " &
    > RealToDouble.To String, MsgBoxStyle.Cri tical)
    > Return 0
    > End Try
    >
    > End Function
    >
    > Thanks for your help
    > David[/color]


    Comment

    • Cor

      #3
      OT: Re: Convert VB Double to Pascal Real

      Hi OHM

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

      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


      Comment

      • One Handed Man

        #4
        Re: Re: Convert VB Double to Pascal Real

        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
        please.

        Thanks - OHM

        Cor wrote:[color=blue]
        > Hi OHM
        >
        > Can you take a look at that answer from cc I have sended in and give
        > some comments.
        >
        > 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[/color]


        Comment

        • Cor

          #5
          OT: Repost posible Bug

          Hi OHM this was a question from cc
          [color=blue]
          > how do I make keyboard & mouse temporary not response to desktop[/color]

          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(B yVal sender As System.Object, _
          ByVal e As System.EventArg s) 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
          Threading.Threa d.Sleep(50)
          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
          ///



          Comment

          • One Handed Man

            #6
            Re: Repost posible Bug

            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:[color=blue]
            > Hi OHM this was a question from cc
            >[color=green]
            >> how do I make keyboard & mouse temporary not response to desktop[/color]
            >
            > 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(B yVal sender As System.Object, _
            > ByVal e As System.EventArg s) 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
            > Threading.Threa d.Sleep(50)
            > 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
            > ///[/color]


            Comment

            • Cor

              #7
              Re: Repost posible Bug

              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


              Comment

              • David Scemama

                #8
                Re: Convert VB Double to Pascal Real

                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" <Bombay@Duck.ne t> wrote in message
                news:bpvjji$t6n $1@titan.btinte rnet.com...[color=blue]
                > I probably have not understood you correctly, but can you not simply[/color]
                convert[color=blue]
                > 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[/color]
                around.[color=blue]
                > 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:[color=green]
                > > 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
                > > "DoubleToRe al (ByVal Data As Double) As String"
                > >
                > > Here is the conversion from real to double:
                > >
                > > Public Function RealToDouble(By Val 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(Rea lToDouble), 2))
                > > Catch ex As Exception
                > > 'MsgBox("RealTo Double, Conversion error: " & Data & "; " &
                > > RealToDouble.To String, MsgBoxStyle.Cri tical)
                > > Return 0
                > > End Try
                > >
                > > End Function
                > >
                > > Thanks for your help
                > > David[/color]
                >
                >[/color]


                Comment

                • One Handed Man

                  #9
                  Re: Repost posible Bug

                  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:[color=blue]
                  > Hi OHM this was a question from cc
                  >[color=green]
                  >> how do I make keyboard & mouse temporary not response to desktop[/color]
                  >
                  > 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(B yVal sender As System.Object, _
                  > ByVal e As System.EventArg s) 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
                  > Threading.Threa d.Sleep(50)
                  > 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
                  > ///[/color]


                  Comment

                  • Cor

                    #10
                    Re: Repost posible Bug

                    But should it do that?


                    Comment

                    • One Handed Man

                      #11
                      Re: Repost posible Bug

                      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:[color=blue]
                      > But should it do that?[/color]


                      Comment

                      • Cor

                        #12
                        Re: Convert VB Double to Pascal Real

                        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
                        value, maybe somebody can help you than.

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

                        Cor


                        Comment

                        • Cor

                          #13
                          Re: Repost posible Bug

                          Hi OHM,

                          Putted it in the vb.language.con trols group.

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

                          :-))

                          Cor


                          Comment

                          • David Scemama

                            #14
                            Re: Convert VB Double to Pascal Real

                            The exact representation of the Pascal Real type is:
                            Sign Significand Exponent
                            Width (bits) 1 39 8

                            David

                            "David Scemama" <david.scemama@ nospam.wanadoo. fr> wrote in message
                            news:eFNXol2sDH A.2492@TK2MSFTN GP12.phx.gbl...[color=blue]
                            > The Pascal Real type, is stored on 6 bytes with a very special coding.[/color]
                            When[color=blue]
                            > you write a real value in a file, 6 bytes are written to represent the[/color]
                            value[color=blue]
                            > (obviously, no language writes the string representation of the real !).[/color]
                            My[color=blue]
                            > function reads the 6 bytes and convert them to a VB double.
                            >
                            > I need the reverse function !
                            >
                            > David
                            >
                            >
                            > "One Handed Man" <Bombay@Duck.ne t> wrote in message
                            > news:bpvjji$t6n $1@titan.btinte rnet.com...[color=green]
                            > > I probably have not understood you correctly, but can you not simply[/color]
                            > convert[color=green]
                            > > the double to a string an then truncate the string to the correct[/color][/color]
                            length?[color=blue][color=green]
                            > >
                            > > 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[/color]
                            > around.[color=green]
                            > > 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:[color=darkred]
                            > > > 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
                            > > > "DoubleToRe al (ByVal Data As Double) As String"
                            > > >
                            > > > Here is the conversion from real to double:
                            > > >
                            > > > Public Function RealToDouble(By Val 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(Rea lToDouble), 2))
                            > > > Catch ex As Exception
                            > > > 'MsgBox("RealTo Double, Conversion error: " & Data & "; " &
                            > > > RealToDouble.To String, MsgBoxStyle.Cri tical)
                            > > > Return 0
                            > > > End Try
                            > > >
                            > > > End Function
                            > > >
                            > > > Thanks for your help
                            > > > David[/color]
                            > >
                            > >[/color]
                            >
                            >[/color]


                            Comment

                            • One Handed Man

                              #15
                              Re: Repost posible Bug

                              You'll probably get the reply in German !.

                              Regards - OHM

                              Cor wrote:[color=blue]
                              > Hi OHM,
                              >
                              > Putted it in the vb.language.con trols group.
                              >
                              > Almost the private group from Herfied, I am curious what happens.
                              >
                              > :-))
                              >
                              > Cor[/color]


                              Comment

                              Working...