VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



File Conversion to Bytes and String

by Sathyanarayanan K (1 Submission)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Wed 4th October 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)

File Conversion to Bytes and String

API Declarations


' 2 functions to write the file as Bytes and another to String
' 2 functions to read the file as bytes and other as string

Rate File Conversion to Bytes and String



' Purpose: ReadByte Array from Binary File
' Remarks: Converting a file to binary file in Bytes()

Private Function ReadByteArrayFromBinaryFile(ByVal Filename As String) As Byte()
    Dim FileNo As Integer, FileSize As Long
    Dim ByteArray() As Byte
    
    FileNo = FreeFile
    Open Filename For Binary Access Read Shared As #FileNo
    'Open Filename For Input As #FileNo
        FileSize = LOF(FileNo)  ' get the file size
        
        ReDim ByteArray(1 To FileSize)
        
        Get #FileNo, , ByteArray()
        
        ReadByteArrayFromBinaryFile = ByteArray()
        
    Close #FileNo
End Function


' Purpose: WriteByte Array To Binary File
' Remarks: 

Private Sub WriteByteArrayToBinaryFile(ByVal Filename As String, ByRef ByteArray() As Byte)
    Dim FileNo As Integer
    
    FileNo = FreeFile ' get next available file number
    Open Filename For Binary Access Write Shared As #FileNo
        
        Put #FileNo, , ByteArray()
        
    Close #FileNo
End Sub


' Purpose: Read Binary File to String
' Remarks: Convert to String datatype

Private Function ReadBinaryFile(ByVal Filename As String) As String
    Dim FileNo As Integer, FileSize As Long
    Dim i As Long, Loops As Long, Remainder As Long
    Dim Buffer As String
    Dim BinaryData As String
    
    Const BUFFER_SIZE As Integer = 30000
    
    FileNo = FreeFile
    Open Filename For Binary Access Read Shared As #FileNo
        
        FileSize = LOF(FileNo)  ' get the file size
        Loops = Int(FileSize / BUFFER_SIZE)  ' calculate number of loops required
        
        For i = 1 To Loops
            Buffer = String$(BUFFER_SIZE, Chr$(0))
            Get #FileNo, , Buffer
            BinaryData = BinaryData & Buffer
        Next
        
        ' fill in the leftovers
        '
        Remainder = FileSize Mod BUFFER_SIZE
        If Remainder > 0 Then
            Buffer = String$(Remainder, Chr$(0))
            Get #FileNo, , Buffer
            BinaryData = BinaryData & Buffer
        End If
        
        ReadBinaryFile = BinaryData
        
    Close #FileNo
End Function



' Purpose: Write Binary File
' Remarks: Convert to String datatype

Private Sub WriteBinaryFile(ByVal Filename As String, ByVal BinaryData As String)
    Dim FileNo As Integer, FileSize As Long
    Dim i As Long, Loops As Long, Remainder As Long
    Dim Buffer As String
    
    Const BUFFER_SIZE As Integer = 30000
    
    FileNo = FreeFile ' get next available file number
    Open Filename For Binary Access Write Shared As #FileNo
        
        Loops = Int(Len(BinaryData) / BUFFER_SIZE)   ' calculate number of loops
        
        For i = 1 To Loops
            Buffer = String$(BUFFER_SIZE, Chr$(0))
            Buffer = Mid$(BinaryData, (BUFFER_SIZE * (i - 1)) + 1, BUFFER_SIZE)
            Put #FileNo, , Buffer
            Buffer = ""
        Next
        
        ' fill in the leftovers
        '
        Remainder = Len(BinaryData) Mod BUFFER_SIZE
        If Remainder > 0 Then
            Buffer = String$(Remainder, Chr$(0))
            Buffer = Right(BinaryData, Remainder)
            Put #FileNo, , Buffer
            Buffer = ""
        End If
        
    Close #FileNo
End Sub


Download this snippet    Add to My Saved Code

File Conversion to Bytes and String Comments

No comments have been posted about File Conversion to Bytes and String. Why not be the first to post a comment about File Conversion to Bytes and String.

Post your comment

Subject:
Message:
0/1000 characters