Results 1 to 9 of 9

Thread: [VB6] clsStrToIntArray.cls - Cast String To Integer Array

Threaded View

  1. #1

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Arrow [VB6] clsStrToIntArray.cls - Cast String To Integer Array

    This simple class makes it very easy to typecast a String into an Integer array. Treating a String as an array enables some kinds of String processing to be done much quicker than is possible with VB's intrinsic String functions.


    clsStrToIntArray.cls
    Code:
    
    Option Explicit
    
    Private Const FADF_AUTO      As Integer = &H1   'An array that is allocated on the stack.
    Private Const FADF_FIXEDSIZE As Integer = &H10  'An array that may not be resized or reallocated.
    
    Private Type SAFEARRAY1D    'Represents a safe array. (One Dimensional)
        cDims      As Integer   'The count of dimensions.
        fFeatures  As Integer   'Flags used by the SafeArray.
        cbElements As Long      'The size of an array element.
        cLocks     As Long      'The number of times the array has been locked without a corresponding unlock.
        pvData     As Long      'Pointer to the data.
        cElements  As Long      'The number of elements in the dimension.
        lLbound    As Long      'The lower bound of the dimension.
    End Type                    'http://msdn.microsoft.com/en-us/library/ms221482(v=vs.85).aspx
    
    Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ByRef ArrayVar() As Any) As Long
    Private Declare Sub PutMem4 Lib "msvbvm60.dll" (ByVal Addr As Long, ByVal NewVal As Long)
    
    Private Ptr  As Long
    Private SA1D As SAFEARRAY1D
    
    Private Sub Class_Initialize()
        With SA1D
            .cDims = 1
            .fFeatures = FADF_AUTO Or FADF_FIXEDSIZE
            .cbElements = 2&
            .cLocks = 1&
            .lLbound = 1&
        End With
    End Sub
    
    'This should be the first method called right after instantiating
    'the class and should be invoked only once per class instance.
    'Pass the Integer array that will substitute for the String.
    
    Public Sub InitArray(ByRef IntArray_OUT() As Integer)
        Erase IntArray_OUT
        Ptr = VarPtrArray(IntArray_OUT())
        PutMem4 Ptr, VarPtr(SA1D)
    End Sub
    
    'This function typecasts the passed String into an Integer array.
    'That is, the characters of the String can be treated as elements
    'of the Integer array. Any number of Strings can be typecast to
    'the Integer array by calling this function repeatedly. However,
    'the array should not be Erased when assigning another String.
    'This function fails (returns False) if passed an empty string.
    
    Public Function CastString(ByRef String_IN As String) As Boolean
        Dim StrLen As Long
    
        If Ptr Then
            StrLen = Len(String_IN)
            If StrLen Then
                With SA1D
                   .pvData = StrPtr(String_IN)
                   .cElements = StrLen
                    CastString = .pvData <> 0&
                End With
            End If
        End If
    End Function
    
    Private Sub Class_Terminate()
        If Ptr Then PutMem4 Ptr, 0&
    End Sub
    
    
    modMain.bas
    Code:
    
    Option Explicit
    
    Private Sub Main()
        Dim aintChars() As Integer, i As Long
        Dim sControlChars As String, sPrintableChars As String
    
        sControlChars = Space$(31&)
        sPrintableChars = String$(224&, 0)
    
        With New clsStrToIntArray
           .InitArray aintChars()
    
            If .CastString(sPrintableChars) Then
                For i = LBound(aintChars) To UBound(aintChars)
                    aintChars(i) = i + 31&
                Next
                Debug.Print """" & sPrintableChars & """"
            End If
    
            If .CastString(sControlChars) Then
                For i = LBound(aintChars) To UBound(aintChars)
                    aintChars(i) = i
                Next
                Debug.Print """" & sControlChars & """"
            End If
        End With
    End Sub
    
    
    Attached Files Attached Files
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width