[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

long integer conversion in VB3

Posted on 1998-08-03
9
Medium Priority
?
342 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 150 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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…
Suggested Courses
Course of the Month20 days, 13 hours left to enroll

864 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