Solved

sscanf function in VB?

Posted on 1998-05-18
8
1,716 Views
Last Modified: 2011-03-21
Is there a function in VB that is simmilar to the C function sscanf?
0
Comment
Question by:hubermar
  • 5
  • 2
8 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
ID: 1452832
There is no directly corresponding function but you can manipulate a string and convert text to numbers. If your string was of the form "FRED 1234 XYZ". you could write a sub such as

sub(text1 as string,numeric1 as double,text2 as string,target as string)
dim i as integer
dim length as integer
dim text3 as string

i = 1
length = len(target)

do until i > length or target(i) = " "
    text1 = text1 + target(i)
loop

i = i + 1

do until i > length or target(i) = " "
    text3 = text3 + target(i)
loop
numeric1 = val(text3)

i = i+1
     
do until i > length or target(i) = " "
    text2 = text2 + target(i)
loop
 
end sub
     
You'd have to tailor individual functions for individual circumstances - You could probably use variants for the variables and then select appropriate translation by 'chopping up'  a format string in the form "%d %s %d" etc

0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1452833
No huberma, there is no function in VB is similar to sscanf, and I didn't find any API function equivalent to it, but you can use the normal string manipulation functions instead (like InStr, Mid, Left...).

Bin Huwairib
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452834
CORRECTION TO STRING MANIPULATION

    sub(text1 as string,numeric1 as double,text2 as string,target as string)
    dim i as integer
    dim length as integer
    dim text3 as string

    i = 1
    length = len(target)

    do until i > length or mid(target,i,1) = " "
        text1 = text1 + mid(target,i,1)
    loop

    i = i + 1

    do until i > length or mid(target,i,1) = " "
        text3 = text3 + mid(target,i,1)
    loop
    numeric1 = val(text3)

    i = i+1
         
    do until i > length or mid(target,i,1) = " "
        text2 = text2 + mid(target,i,1)
    loop
     
    end sub
0
 

Author Comment

by:hubermar
ID: 1452835
deighton,

That's the method that I'm currently using.  It's a real pain.  I appreciate your suggestion.

Thanks,
Mark
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 18

Expert Comment

by:deighton
ID: 1452836
What are you trying to do exactly?  I can think of ways of improving my function.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452837
'Here's a more complcated example - hope it helps


Sub sscanf(ByVal sBuff As String, ByVal sFormat As String, Optional a, Optional b, Optional c, Optional d)

'Attempt to bring Sscanf function into VB
'uses optional variant parameters
'MAX four parameters in scanf string - but this COULD BE INCREASED

'EXAMPLE sscanf("1234 1.2 6","%i %d %i",a,b,c)
' puts 1234 in a (a is integer)
'        1.2 in b (d is a double)
'       6   in c (c is an integer)

Dim sTemp As String
Dim sID As String
Dim i As Integer
Dim i2 As Integer

'Extract paramater a

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, a)

If IsMissing(b) Then Exit Sub

sBuff = Right(sBuff, Len(sBuff) - i - 1)
sFormat = Right(sFormat, Len(sFormat) - i2 - 1)

'extract parameter b

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, b)

If IsMissing(c) Then Exit Sub

'You would repeat the sections to add new parameters e,f,g,... and put them in the parameter list


sBuff = Right(sBuff, Len(sBuff) - i - 1)
sFormat = Right(sFormat, Len(sFormat) - i2 - 1)

'extract parameter c

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, c)

If IsMissing(d) Then Exit Sub

sBuff = Right(sBuff, Len(sBuff) - i - 1)
sID = Right(sFormat, Len(sFormat) - i2 - 1)

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, d)

End Sub

Sub strip(sBuff As String, iReturn As Integer, sStrip As String)
    'Strip the next value or control code out of the string

    Dim i As Integer

    i = 1
    Do Until i > Len(sBuff) Or Mid(sBuff, i, 1) = " "
        i = i + 1
    Loop
   
    sStrip = Left(sBuff, i - 1)
    iReturn = i - 1

End Sub


Sub value(sExtract As String, sFormat As String, vResult)
   
    If sFormat = "%i" Then
        vResult = Int(Val(sExtract))
    ElseIf sFormat = "%s" Then
        vResult = sExtract
    ElseIf sFormat = "%d" Then
        vResult = Val(sExtract)
    End If
 
End Sub


'EXAMPLE

Private Sub Command1_Click()

    'example function call

    Dim i As Integer
    Dim s As String
    Dim d As Single
    Dim d2 As Double
    Call sscanf("1234 FRED 1592654 1", "%i %s %d %d", i, s, d, d2)
    MsgBox i
    MsgBox s
    MsgBox d
    MsgBox d2
End Sub

0
 

Author Comment

by:hubermar
ID: 1452838
deighton,

Very impressive.  I thank you for your help.  I will include this in my code.  I wasn't expecting so much help.  You went the extra mile to answer my question.  

Is it permissible to notify you when I ask questions in the future?  

Thanks again,
Mark
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452839
E-Mail

DEIGHA@GUSCO.COM
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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

743 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

11 Experts available now in Live!

Get 1:1 Help Now