Solved

long integer conversion in VB3

Posted on 1998-08-03
9
325 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

813 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

13 Experts available now in Live!

Get 1:1 Help Now