Solved

long integer conversion in VB3

Posted on 1998-08-03
9
321 Views
Last Modified: 2012-05-04
How do I extract a long integer from a file (stored using MKL$ in MsBasic 7,.2) and store the same long back to the file.
I've tried extrecting it with :
           dim a as integer
           dim data as string * 6
           a& = Clng(left(data$,4))

but it returns Type Mismatch
0
Comment
Question by:ozlem
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 14

Expert Comment

by:waty
ID: 1467575
You should declare the variable 'a' as long, and you have to care that all first 4 characters in 'data' are numeric.

dim a as integer
dim data as string * 6

a& = Clng(left(data$,4))

To save the variable back to a file, you could use the Put statment.
0
 
LVL 2

Expert Comment

by:swilt
ID: 1467576
I was just preparing this to answer the question, I found it was already answered but I thought I would post this anyway

Option Explicit

Private Sub Form_Load()
    Dim a As Long
    Dim data As String * 6
   
    'My test data
    data = Chr$(&H7) & Chr$(&H5B) & Chr$(&HCD) & Chr$(&H15)
   
    a = nConvToLong(Left$(data, 4))
   
    MsgBox a
End Sub

Private Function nConvToLong(ByVal sInp As String) As Long
    Dim nRet As Long
    Dim i As Integer
   
    nRet = 0
    If Len(sInp) > 0 Then
        For i = 1 To Len(sInp)
            nRet = nRet * 256
            nRet = nRet + Asc(Mid$(sInp, i, 1))
          '  MsgBox Hex$(nRet)
        Next i
    End If
    nConvToLong = nRet
End Function

0
 

Author Comment

by:ozlem
ID: 1467577
Dear Waty,

Sorry I meant to declare it as a Long Integer but it still won't work. As it is a Long, the 4 Bytes are therefore not numeric

Dear Swilt

Lovely bit of coding but I can't get it to work.
0
 
LVL 14

Expert Comment

by:waty
ID: 1467578
If your four bytes are not numeric, how do you want to convert them in long?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ozlem
ID: 1467579
Dear Waty,

In MSBasic an MKL$ converts a numeric to a long i.e. four bytes

What I have now discovered, with the aid of swilt, is that is is held as 4 bytes in reverse, so that reversing the original 4 byte s (byte 4,3,2 and 1 to byte 1,2,3 and 4) and taking the ascii value and multiplying by 256 give the converted number.

0
 

Author Comment

by:ozlem
ID: 1467580
Dear Swilt,

You got answer right but didn't explain that I had to reverse the original Long Integer. So I think you should get the points but I don't know how to release them.

Watch out for the same questions but the singles and doubles.

Many Thanks
0
 
LVL 2

Accepted Solution

by:
swilt earned 50 total points
ID: 1467581
You could just give me the points in reply to this
0
 
LVL 2

Expert Comment

by:swilt
ID: 1467582
p.s.

Use  For i = Len(sInp) to 1 step -1 if the number is round the other way and watch out for negative numbers because I have not tested for them
0
 
LVL 9

Expert Comment

by:cymbolic
ID: 1467583
A long is just four bytes as a character string.  Here's an old inline function that I wrote in Quick Basic to work with a long value that I had read in as four bytes:

DEF FNS2L& (s$)
hex2& = ASC(MID$(s$, 2, 1))
hex2& = hex2& * 256
hex3& = ASC(MID$(s$, 4, 1))
hex3& = hex3& * 4096
hex4& = ASC(MID$(s$, 3, 1))
hex4& = hex4& * 65536
FNS2L& = (ASC(MID$(s$, 1, 1))) + hex2& + hex3& + hex4&
END DEF

To use this you would read in the data into a four byte string variable.  Using binary file i/o and assuming your integer is at the beginning of the file that would be simply:

open "FileName" for binary as #1
x$=space$(4)
get #1,,x$
close #1
and say you convert the above to a basic function:
lngvar&=FNS2l&(x$)

Under Quick Basic these strings were more easily handled through the availability of the MKL$, CVL instructions now missing from VB.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now