Android Question Language Barrier - Fixed width strings.

RickV

Member
Licensed User
Topic is ZModem. I have written a zmodem protocol back in VB6 days and need to convert to B4A and B4J. It was a very successful two way protocol that recovers from errors and continues without having to start a new download. ie: incomplete . I am aware that there is ftp available in b4x however zmodem over tcp connection is required.

I cannot work out how to have fixed width strings in b4x. Below is some code from vb6 to demonstrate what I require to achieve. As you will see this is set to a specific width or chunk size........

VB6 Global Code:
    Global c2048    As String * 2048
    Global c1024    As String * 1024
    Global c512     As String * 512
    Global c256     As String * 256
    Global c128     As String * 128
    Global c64      As String * 64
    Global c32      As String * 32
    Global c16      As String * 16
    Global c8       As String * 8
    Global c4       As String * 4
    Global c2       As String * 2
    Global c1       As String * 1

So after having our chunk size empty strings, we then pull a chunk from a file that we wish to transfer. The following VB function I wrote will retrieve the required next chunk and return the data as a string ready to poke down the tcp ip connection.

VB6 Finction Code:
Function ZModem(TheFileName As String, Record As Long) As String

Dim RecNum      As Long
Dim TheFile     As Integer
Dim OutString   As String
Dim TotalBytes  As Long


TheFile = FreeFile
Open TheFileName For Binary Shared As #TheFile
Seek TheFile, 1
CPXLen = LOF(TheFile)
TotalBytes = CPXLen
mn = 0

If CPXLen > 0 Then
RecNum = 0
    Do 'While CPXLen > 0
        If CPXLen >= 2048 Then
            Get #1, , c2048
            OutString = c2048
            RecNum = RecNum + 1
            CPXLen = CPXLen - 2048
            mn = mn + 2048
        ElseIf CPXLen >= 1024 Then
            Get #1, , c1024
            OutString = c1024
            RecNum = RecNum + 1
            CPXLen = CPXLen - 1024
            mn = mn + 1024
        ElseIf CPXLen >= 512 Then
            Get #1, , c512
            OutString = c512
            RecNum = RecNum + 1
            CPXLen = CPXLen - 512
            mn = mn + 512
        ElseIf CPXLen >= 256 Then
            Get #1, , c256
            OutString = c256
            RecNum = RecNum + 1
            CPXLen = CPXLen - 256
            mn = mn + 256
        ElseIf CPXLen >= 128 Then
            Get #1, , c128
            OutString = c128
            RecNum = RecNum + 1
            CPXLen = CPXLen - 128
            mn = mn + 128
        ElseIf CPXLen >= 64 Then
            Get #1, , c64
            OutString = c64
            RecNum = RecNum + 1
            CPXLen = CPXLen - 64
            mn = mn + 64
        ElseIf CPXLen >= 32 Then
            Get #1, , c32
            OutString = c32
            RecNum = RecNum + 1
            CPXLen = CPXLen - 32
            mn = mn + 32
        ElseIf CPXLen >= 16 Then
            Get #1, , c16
            OutString = c16
            RecNum = RecNum + 1
            CPXLen = CPXLen - 16
            mn = mn + 16
        ElseIf CPXLen >= 8 Then
            Get #1, , c8
            OutString = c8
            RecNum = RecNum + 1
            CPXLen = CPXLen - 8
            mn = mn + 8
        ElseIf CPXLen >= 4 Then
            Get #1, , c4
            OutString = c4
            RecNum = RecNum + 1
            CPXLen = CPXLen - 4
            mn = mn + 4
        ElseIf CPXLen >= 2 Then
            Get #1, , c2
            OutString = c2
            RecNum = RecNum + 1
            CPXLen = CPXLen - 2
            mn = mn + 2
        ElseIf CPXLen >= 1 Then
            Get #1, , c1
            OutString = c1
            RecNum = RecNum + 1
            CPXLen = CPXLen - 1
            mn = mn + 1
        End If
        If CPXLen = 0 And (RecNum < Record) Then
            GoTo 100
            Exit Do
        End If
    Loop While RecNum < Record
    OutString = FillIt(Format(RecNum, "0"), 1, 8) + _
                FillIt(Format(TotalBytes, "0"), 1, 8) + _
                OutString
Else
'    OutString = FillIt(Format(RecNum, "0"), 1, 8) + OutString
100 OutString = ""
    OutString = FillIt(Format(0, "0"), 1, 8) + _
                FillIt(Format(TotalBytes, "0"), 1, 8) + _
                OutString
End If
Close #TheFile
   
ZModem = OutString + CRC(OutString)
    
End Function

the two questions I have, is
1. How to get a fixed width string and
2. Are we able to open a file for binary random access like in the function above ?

Any help would be greatly appreciated.
 

RickV

Member
Licensed User
I have come up with this so far....

B4X:
Dim aaa As String
    Dim RAF As RandomAccessFile
    Dim b() As Byte
    Dim x() As Byte
            
    TXRec = Rec
    RAF.Initialize(File.DirInternalCache, fName, True)
    RAF.CurrentPosition = Rec
    If RAF.Size - (RAF.CurrentPosition + 4096) > 4095 Then
            RAF.ReadBytes(b, 0, 4096, TXRec)
            TXRec = TXRec + 4096
            aaa = BytesToString(x, 0, x.Length, CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 2048) > 2047 Then
            RAF.ReadBytes(b, 0, 2048, TXRec)
            TXRec = TXRec + 2048
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 1024) > 1023 Then
            RAF.ReadBytes(b, 0, 1024, TXRec)
            TXRec = TXRec + 1024
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 512) > 511 Then
            RAF.ReadBytes(b, 0, 512, TXRec)
            TXRec = TXRec + 512
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 256) > 255 Then
            RAF.ReadBytes(b, 0, 256, TXRec)
            TXRec = TXRec + 256
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 128) > 127 Then
            RAF.ReadBytes(b, 0, 128, TXRec)
            TXRec = TXRec + 128
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 64) > 63 Then
            RAF.ReadBytes(b, 0, 64, TXRec)
            TXRec = TXRec + 64
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 32) > 31 Then
            RAF.ReadBytes(b, 0, 32, TXRec)
            TXRec = TXRec + 32
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 16) > 15 Then
            RAF.ReadBytes(b, 0, 16, TXRec)
            TXRec = TXRec + 16
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 8) > 7 Then
            RAF.ReadBytes(b, 0, 8, TXRec)
            TXRec = TXRec + 8
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 4) > 3 Then
            RAF.ReadBytes(b, 0, 4, TXRec)
            TXRec = TXRec + 4
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 2) > 1 Then
            RAF.ReadBytes(b, 0, 2, TXRec)
            TXRec = TXRec + 2
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 1) > 0 Then
            RAF.ReadBytes(b, 0, 1, TXRec)
            TXRec = TXRec + 1
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 1) > RAF.Size  Then
            SendIt(18551, Pad(fName,20,0))
        End If
    'End If
    RAF.Close
 
Upvote 0
Top