Android Question trouble with bc.LongsToBytes

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
B4X:
    Dim v(1) As Long
    v(0) = 200
    Dim b() As Byte = bc.LongsToBytes(v)



... how can a byte contain a long?

B4X:
Dim v3 As Int = b(7) + 256
... this helps, but maybe doesn't care the reason


B4X:
    Dim v3 As Long = val

    v3 = Bit.UnsignedShiftRight(v3,1)

Bit.UnsignedShiftRight seems to work with long as well, not only with int as declared. Very usefull
 
Last edited:

Semen Matusovskiy

Well-Known Member
Licensed User
In Java a byte represents a value between −128 - 127 (signed).
200 (dec) = 0xC8 (hex).
0xC8 like unsigned is 200, like signed -56 (256 - 200)
 
Upvote 0

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
so it is a java specific thing. Is there any way to keep the result unsigned or does it work by tricks only, wehen i operate bytes and ints? Need it for midi straming...
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
For typical operations with bytes the sign is not important (for example, reading a file into array of bytes or conversion string to bytes). So tricks are not needed.
Of course, a math can be problematic.
Which operations confuse you ?
 
Upvote 0

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
a long needs to be represented in bytes, but only 7bit per byte, because most significant bit is signal. And it should work fast. So I am unshure, if I prefere binary string operations with substrings or maths.

B4X:
private Sub midiDeltaFromDez (ticks As Long) As Long
 
private Sub midiDeltaFromDez (ticks As Long) As Long
   
    Dim long1, long2, long3 As Long
    Dim tickStr As String = ticks
    Dim bitstr As String = getBitStr(ticks)
    Dim bitbyte() As Byte =  bitstr.GetBytes("utf8")
    Dim bitbyte2(bitbyte.Length) As Byte
    For i = 0 To bitbyte.Length - 1
        bitbyte2(i) = bitbyte(bitbyte.Length - i - 1)
    Next
   
   
    'If Bit.
    'bc.
    If  ticks < 128 Then
        For i = 0 To 6
            If bitbyte2(i)= 49 Then
                long1 = long1  +  Power( 2,i)
            End If
        Next
       
        ' 1 byte
    else If ticks < 16384 Then
        ' 2 byte
        For i = 0 To 6
            If bitbyte2(i)= 49 Then
                long1 = long1  +  Power( 2,i)
            End If
        Next
        For i = 7 To bitbyte.Length - 1
            If bitbyte2(i)= 49 Then
                long2 = long2  +  Power( 2,i)
            End If
        Next
        'long2 = long2  + 128
       
    Else If ticks < 2097152 Then
        ' 3 byte
        'int1 = 
        For i = 0 To 6
            If bitbyte2(i)= 49 Then
                long1 = long1  +  Power( 2,i)
            End If
        Next
        For i = 7 To 13
            If bitbyte2(i)= 49 Then
                long2 = long2  +  Power( 2,i)
            End If
        Next
    Else
        For i = 0 To 6
            If bitbyte2(i)= 49 Then
                long1 =long1+  Power( 2,i)
            End If
        Next
        For i = 7 To 13
            If bitbyte2(i)= 49 Then
                long2 = long2  +  Power( 2,i)
            End If
        Next
        For i = 7 To 13
            If bitbyte2(i)= 49 Then
                long3 = long3  +  Power( 2,i)
            End If
        Next
       
    End If
   
End Sub
 
End Sub
 
Last edited:
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I don't see dangerous operations with bytes (maybe I lost something).
Comparasion (=48, =49) and moving (bitbyte2(i) = bitbyte(bitbyte.Length - i - 1)) only.
 
Upvote 0

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
Yes, this would be a konservativ solution. The delay could be satisfactory because in fast action, the ticks are short and only needs the smaller algorithm.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…