[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

long integer conversion in VB3

Posted on 1998-08-03
9
Medium Priority
?
338 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
Independent Software Vendors: 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!

 
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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Suggested Courses

656 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