Android Question Convert Byte() to Short()

Midimaster

Active Member
Licensed User
I'm still looking for a performant way to load a file with 50.000.000 signed shorts. Now I'm testing loading BYTEs, then coverting it to SHORTs. Trying to convert a big Byte-Array into a big Short Array

if the element size is 100.000 the speed is very fast: <10msec

if I try to do it with 300.000 elements the time is extremly high >5000msec
B4X:
    Private zeit As Long = DateTime.now
    Private buffer() As Byte
    Private Value(2200000) As Short
    buffer = File.ReadBytes(File.DirAssets,"Myfile.BIN")

    Log("ZEIT A=" & (DateTime.now-zeit))
    Log("BUFFER SIZE= " & buffer.Length)
    For i= 0 To 300000 'buffer.length/2-2
        Value(i)=buffer(2*i+1)*256+buffer(2*i)
    Next
    Log("ZEIT B=" & (DateTime.now-zeit))

The loading time is 70msec only, but the converting seems to cause the problem. What ca I do?


Some additional Test:

if I try this, the time is again >1400msec
B4X:
...
buffer = File.ReadBytes(File.DirAssets,"ShortExport.MCA")

...
Private xxx as Short
    For i= 0 To 300000 'buffer.length/2-2
        Value(i)=0
    Next
    Log("ZEIT B=" & (DateTime.now-zeit))




if I try this, the time is <10msec
B4X:
...
...
Private xxx as Short
    For i= 0 To 300000 'buffer.length/2-2
        xxx=buffer(0)
    Next
    Log("ZEIT B=" & (DateTime.now-zeit))

So it looks like the calculating and the storing are the problems
 
Last edited:

Midimaster

Active Member
Licensed User
At the moment I'm testing only in DEBUG mode. Do you expect massiv changes in RELEASE? Where can I change the mode. I'm using B4A Bridge to transfer the app.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
1593086349778.png


You can change it in the drop down menu, change it to Release .
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Value(i)=buffer(2*i+1)*256+buffer(2*i)
I don't think this is correct, see:
B4X:
    Dim bytes(2) As Byte = Array As Byte (-128, -128)
    Dim value As Short
    Dim value2 As Short
    value=bytes(1)*256+bytes(0)
    Log($"value=${value}"$)
    value2 = Bit.Or(Bit.ShiftLeft(bytes(1),8), Bit.And(bytes(0), 0xFF))
    Log($"value2=${value2}"$)
Output:
value=32640
value2=-32640

Update: @stevel05's recommendation is best for this scenario (and it also produces -32640)
B4X:
    Dim bc As ByteConverter
    Dim value3() As Short = bc.ShortsFromBytes(bytes)
    Log($"value3(0)=${value3(0)}"$)
Output:
value3(0)=-32640
 
Last edited:
Upvote 0

Midimaster

Active Member
Licensed User
Upps... Bytes in an Byte-Array can have negative values?

thank you all. I did not recognize that BYTE() can have negative values. I'm used to old styles BYTES only from 0 to 255. Thank you, then I have to check the data source too!

You all help me surviving the first days with this new language. Step by step I'm getting better...
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upps... Bytes in an Byte-Array can have negative values?
It's all up to a programming languages interpretation. B4J/B4A/B4i have only signed bytes. Does not mean that bit-wise a signed byte differs from an unsigned byte, but when it comes to operations performed on them (such as addition an multiplication, even shifting) in B4X, it makes a difference.
 
Upvote 0
Top