473,756 Members | 5,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 20 '05 #1
17 4374
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
"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

Nov 20 '05 #2
Cor
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
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
please.

Thanks - OHM

Cor wrote:
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

Nov 20 '05 #4
Cor
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(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
///

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(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
///

Nov 20 '05 #6
Cor
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.ne t> wrote in message
news:bp******** **@titan.btinte rnet.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
"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


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(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
///

Nov 20 '05 #9
Cor
But should it do that?
Nov 20 '05 #10

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

Similar topics

17
3020
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 in the field and save the record to the file. When I look in the binary file (with python or an hex editor), the field is stored on 8 bytes: 00-00-00-00-00-00-7F-00. I try unpack from struct module but the result isn't good.
5
6031
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
16545
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 want the result of shortVal = 3000 and store as short instead of double
28
2268
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 compiler will simply stop when it is missing an implementation for a procedure or whatever.
2
4962
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 'System.OverflowException' occurred in mscorlib.dll Additional information: Value was either too large or too small for a Double. Pascal
2
1804
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} ------------------------------------------------- Sub licz() Dim i As Integer x = 0 For i = 0 To 2
5
1306
by: karmalax | last post by:
public static string Somme(string T) { string text1 = ""; double num5 = Strings.Len(T) + 1; int num3 = 7;
3
8748
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; this produced an error
54
3162
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 solved them but it could be I overlooked something. 1) In Pascal you can declare functions inside a function. AFAIK this is not possible with C. Or am I wrong?
0
9287
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.