Android Question Binary data with unsigned integers

Dave G

Active Member
Licensed User
I'm working on a proof of concept that pulls in data from medical devices e.g. oximeter and stores that data in a database for subsequent analysis. The data is mostly binary. I've written a C# app on Windows that reads the data in and stores it as binary, csv, database.

Now, I'm working on an Android app (and maybe iOS) that will pull in the binary data and populate a SQLite database. The issue is get the unsigned short/int/long data out of the binary files.

Here's a snippet of what I've encountered:

    Dim aByte As Byte
    Dim aShort As Short
    Dim anInt As Int
    Dim aLong As Long
    rafInput.Initialize(internalDir, "O2M.dat", True)
    ' should be 5
    Dim theVersion As Short = rafInput.ReadShort(rafInput.CurrentPosition) ' 1280
    ' should be 2023
    'Dim theYear As Long = rafInput.ReadShort(rafInput.CurrentPosition) ' -6393
    Dim theYear As Int = rafInput.ReadShort(rafInput.CurrentPosition) ' -6393
    'Dim theYear As Long = ToUnsigned(rafInput.ReadShort(rafInput.CurrentPosition)) ' exception
    'aShort  = rafInput.ReadUnSignedByte(rafInput.CurrentPosition) ' postive 231 vs -25 if byte
I have comments at end of each line to indicate what value I get.

I also used suggested code that produces unsigned values which is called (commented out) and produced an exception:
java.lang.RuntimeException: Method: andLong not found in: b4a.example.b4xmainpage

Unsigned value conversion:
Sub ToUnsigned(i As Int) As Long
    Return AndLong(i, 0xFFFFFFFF)
End Sub
public Sub AndLong (N1 As Long, N2 As Long) As Long
    Return jo.RunMethod("andLong", Array(N1, N2)) ' not found
End Sub

It appears there may be an issue with little/big endian which works with C# and not B4X.
Any help would be appreciated.


Licensed User
Longtime User
The boolean in Initialize is ReadOnly, you are not changing any endian here.

Have you tried to use

Initialize2 (Dir As String, File As String, ReadOnly As Boolean, LittleEndian As Boolean)​

Same as Initialize with the option to set the byte order to little endian instead of the

default big endian. This can be useful when sharing files with Windows computers.

rafInput.Initialize2(internalDir, "O2M.dat", True, True)
Last edited:
Upvote 1