?
Solved

long integer conversion in VB3

Posted on 1998-08-03
9
Medium Priority
?
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 5 hours left to enroll

762 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