Android Question Reading a data file 0-255

ElliotHC

Active Member
Licensed User
Upon looking through the posts in order to find out how in B4A to download and read a file, I have noticed that there may be some issues when importing the data.

In my data file I have 0-255, many of these fall outside of the standard ASCII table, some would call it extended ASCII.

My hardware which is coded using a different Basic compiler, requires that I send it a 2k data bundle created as a file (e.g. 123.ABC) by a VB App I've written.

This data file contains bytes 0-255 in value and I need to read this file which is located on my webserver (http://mydomain.com/123.ABC).

The B4A App which does all the control of the hardware via bluetooth (which is working perfectly), also needs to be able to read the data file and pass it to the hardware through the transparent serial connection that I'm using..

So basically and in as simple form as possible, I need to read from the web location, load that data in to an array that I can parse with the B4A App and send to the hardware.

Erel has given me this example, but I am not sure how to implement it.

B4X:
Dim lines As List = File.ReadList(...)
Dim s As String = File.ReadString(...)
Will that string give me access to the full 0-255 byte?
 

sorex

Expert
Licensed User
for plain data files you need to use the randomaccess library and readbytes().
 

sorex

Expert
Licensed User
This data file contains bytes 0-255 in value
my reply above is focussed on data being real bytes.
Does the quote above mean this? or is it stored as 240,13,167 text? then you can use file.readstring aswell.
 

ElliotHC

Active Member
Licensed User
my reply above is focussed on data being real bytes.
Does the quote above mean this? or is it stored as 240,13,167 text? then you can use file.readstring aswell.
My data is <123><0><255><etc..>

<this is a single byte value>
 

ElliotHC

Active Member
Licensed User
bytes in b4a are signed.

you can convert it to unsigned (0-255) with
B4X:
Sub ToUnsigned(b As Byte) As Int
   Return Bit.And(0xFF, b)
End Sub

B4X:
Sub Download (Callback As Object, link As String) As HttpJob
    Dim j As HttpJob
    j.Initialize("", Callback)
    j.Download("http://mydomain.com/123.ABC")
    Return j
End Sub

Sub Button_Download_Click
    Wait For (Download(Me, "http://mydomain.com/123.ABC")) JobDone (j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release
End Sub
I need to convert j above in to an array, then I can parse through that array and read the bytes as decimal values 0-255 as I need to decrypt it before sending it out to the hardware.
I've done all this already in a VB app using a BT serial interface, but of course it's working a little differently here in B4A.
 

sorex

Expert
Licensed User
download the file with the code in Erel's tutorial I posted above and then read it back in with readbyte into your array.

maybe the saving is not needed but I don't know if you can access the stream data like an array.
 

ElliotHC

Active Member
Licensed User
B4X:
Sub Button_Firmware_Click
    Dim job As HttpJob
    job.Initialize("j", Me)
    job.Download("http://mydomain.com/123.abc")
End Sub

Sub JobDone(job As HttpJob)
    If job.Success Then

        Log(job.GetString)
        
        
        For A1 = 0 To job.GetString.Length
            ' Need to load the array here
        Next

    Else
        Log("Error: " & job.ErrorMessage)
    End If
    job.Release
End Sub

Sub ToUnsigned(b As Byte) As Int
    Return Bit.And(0xFF, b)
End Sub
Something like this?

Although I'm not sure how I use your code in the jobdone sub
 

sorex

Expert
Licensed User
good news... you can access the stream data at once.

this is in B4J

B4X:
Sub Process_Globals
    Private fx As JFX
 Dim bytes(10000) As Byte
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    Dim job As HttpJob
    job.Initialize("j", Me)
    job.Download("http://server-sql/123.abc")
End Sub


Sub JobDone(job As HttpJob)
    If job.Success Then
     job.GetInputStream.ReadBytes(bytes,0,job.GetInputStream.BytesAvailable)
     For x=0 To 10'job.GetInputStream.BytesAvailable-1
       Log( Bit.And(bytes(x),255) )
     Next                                       
   Else
     Log("Error: " & job.ErrorMessage)
   End If
    job.Release
End Sub
 

ElliotHC

Active Member
Licensed User
This Works.

B4X:
Sub JobDone(job As HttpJob)
    If job.Success Then

        Log(job.GetString)
        Dim S As String
        s = job.GetString
        Dim bytes() As Byte = s.GetBytes("UTF8")

        For A1 = 0 To job.GetString.Length - 1
            Log(bytes(A1))
        Next

    Else
        Log("Error: " & job.ErrorMessage)
    End If
    job.Release
End Sub
 

ElliotHC

Active Member
Licensed User
good news... you can access the stream data at once.

this is in B4J

B4X:
Sub Process_Globals
    Private fx As JFX
 Dim bytes(10000) As Byte
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    Dim job As HttpJob
    job.Initialize("j", Me)
    job.Download("http://server-sql/123.abc")
End Sub


Sub JobDone(job As HttpJob)
    If job.Success Then
     job.GetInputStream.ReadBytes(bytes,0,job.GetInputStream.BytesAvailable)
     For x=0 To 10'job.GetInputStream.BytesAvailable-1
       Log( Bit.And(bytes(x),255) )
     Next                                      
   Else
     Log("Error: " & job.ErrorMessage)
   End If
    job.Release
End Sub

Yes, the code I posted only works with ASCII. Let me try that.
 

ElliotHC

Active Member
Licensed User
B4X:
        job.GetInputStream.ReadBytes(bytes,0,job.GetInputStream.BytesAvailable)

        For x = 0 To job.GetInputStream.BytesAvailable - 1
            Log( Bit.And(bytes(x),255))
            Log(ToUnsigned(bytes(x)))
        Next
So in both cases I'm not seeing values that aren't text. I have a <9> in my file in between the characters Any ideas?
 

ElliotHC

Active Member
Licensed User
Scrap that, Filezilla hadn't uploaded the modified data file although it said it had. Working nicely! Thanks guys.
 

ElliotHC

Active Member
Licensed User
For some reason when I have F0 (240) it comes out as -16.. Any ideas? Looks like we are only working with 127

B4X:
        Log(job.GetString)
       
        job.GetInputStream.ReadBytes(bytes,0,job.GetInputStream.BytesAvailable)

        For x = 0 To job.GetInputStream.BytesAvailable - 1
            'Log( Bit.And(bytes(x),255))
            'Log(ToUnsigned(bytes(x)))
            Log(bytes(x))
        Next
upload_2019-5-29_10-31-9.png


upload_2019-5-29_10-31-29.png
 
Top