Insert data length

Private Sub insert_length(savepart As String, pointer As Long)
'a subroutine to insert the length of a data section
 
    Dim tempstringvar As String 'used for temporary storage
    Dim savepartlen As String 'will hold the length after padding 
    Dim a As integer 'used in FOR loop
    Dim gobyte As byte 'used to insert data one byte at a time
    Dim nFileNum As Long 'used in file access
 
    nFileNum = FreeFile
 
    'begin padding
    tempstringvar = Len(savepart) 
    tempstringvar = Hex(tempstringvar) 
    If Len(tempstringvar) < 8 Then 
        For a = Len(tempstringvar) To 7 
            savepartlen = savepartlen & 0 
        Next
    End If
 
    savepartlen = savepartlen & tempstringvar
 
    'begin insertion
    'You must open a file for binary access if one is not already open
    For x = 1 To 4 
        gobyte = "&H" & Right(savepartlen, 2) 
        savepartlen = Left(savepartlen, (Len(savepartlen) - 2)) 
        Put #nFileNum, pointer, gobyte 
        pointer = pointer + 1 
    Next
 
End sub

Usage:

insertlength(mydata, 23) 'will insert the length of mydata which must be either a text string or data from a save with the first byte starting 23 bytes into the file you choose to save to.

You should follow this by inserting the data and moving the pointer on by the length of the data

Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback.

Leave a Reply