Android Question Reading a data file 0-255

Discussion in 'Android Questions' started by ElliotHC, May 29, 2019.

  1. ElliotHC

    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.

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

    sorex Expert Licensed User

    for plain data files you need to use the randomaccess library and readbytes().
     
  3. sorex

    sorex Expert 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.
     
  4. ElliotHC

    ElliotHC Active Member Licensed User

    So

    Code:
    If j.Success Then
            
    Log(j.GetString)
        
    End If
    Would be

    Code:
    If j.Success Then
            
    Log(j.ReadBytes)
        
    End If
    ?
     
  5. ElliotHC

    ElliotHC Active Member Licensed User

    My data is <123><0><255><etc..>

    <this is a single byte value>
     
  6. sorex

    sorex Expert Licensed User

    no, the first one is correct IF the data is plain text
     
  7. sorex

    sorex Expert Licensed User

  8. DonManfred

    DonManfred Expert Licensed User

    bytes in b4a are signed.

    you can convert it to unsigned (0-255) with
    Code:
    Sub ToUnsigned(b As Byte) As Int
       
    Return Bit.And(0xFF, b)
    End Sub
     
    Syd Wright and Jorge M A like this.
  9. ElliotHC

    ElliotHC Active Member Licensed User


    Code:
    Sub Download (Callback As Object, link As StringAs 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.
     
  10. sorex

    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.
     
  11. ElliotHC

    ElliotHC Active Member Licensed User

    Code:
    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
     
  12. DonManfred

    DonManfred Expert Licensed User

    It is really helpful to totally hide all relevant infos.

    What exactly do you get from the call?

    The text from job.getstring is a STRING. There is no array.
     
  13. sorex

    sorex Expert Licensed User

    good news... you can access the stream data at once.

    this is in B4J

    Code:
    Sub Process_Globals
        
    Private fx As JFX
     
    Dim bytes(10000As 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
           LogBit.And(bytes(x),255) )
         
    Next                                       
       
    Else
         
    Log("Error: " & job.ErrorMessage)
       
    End If
        job.Release
    End Sub
     
    Star-Dust likes this.
  14. ElliotHC

    ElliotHC Active Member Licensed User

    This Works.

    Code:
    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
     
  15. ElliotHC

    ElliotHC Active Member Licensed User


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

    DonManfred Expert Licensed User

    if this send -127 - 127 values to the log then you need to use ToUnsigned on the byte.
    Code:
    Log(ToUnsigned(bytes(A1)))
     
  17. ElliotHC

    ElliotHC Active Member Licensed User

    Code:
    job.GetInputStream.ReadBytes(bytes,0,job.GetInputStream.BytesAvailable)

            
    For x = 0 To job.GetInputStream.BytesAvailable - 1
                
    LogBit.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?
     
  18. DonManfred

    DonManfred Expert Licensed User

    You are still HIDING the result you get! WHY?
    Help us help you and post all relevant infos. I´m out for now. not interested to further trying to help
     
  19. ElliotHC

    ElliotHC Active Member Licensed User

    Scrap that, Filezilla hadn't uploaded the modified data file although it said it had. Working nicely! Thanks guys.
     
  20. ElliotHC

    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

    Code:
    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice