Android Question Windows Binary files

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi
before trying to reinvent the wheel, I decided to submit this, maybe silly, question: I'm wondering if it's possible to read binary files written by a Windows program. The question is related to speed, because of size of the files.
Thanks is advance
 

drgottjr

Expert
Licensed User
Longtime User
you can read the bytes in any file. whether or not you know what the bytes mean is another question.
if the bytes are from a .jpg file, then you have to have a way to parse them. you are free to write your
own parsers. operating systems generally use a file's suffix and take action accordingly. but files that
depend on custom software to read them can add any suffix they want. or none at all. you could easily
create a file and store data in it in a format that meant something to you. someone else could easily read
the bytes in that file, but - depending on how clever you were - they could have a very difficult type
ascribing any meaning to those bytes.

it is possible to determine what is in a file by examining the bytes with the aid of a hex editor. some
file types have a "signature" that tells you what the bytes in the file mean. in other words, a .jpg file
could be identified by its "signature" even without a ".jpg" suffix. what you do after that is up to you.

you could write a routine to examine file signatures (a file with many signatures is easily found on the internet).
while you may not know what to do with the file, at least you might know what you were dealing with. you could, eg,
add the appropriate suffix to the file and let the operating system handle the rest
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Thanks to all. My question was bad written. My fault, sorry. I was meaning the following: let's suppose I write some data with Windows, for example an long integer, followed by two floats and six doubles. I will have a "block" made by 4+2x4+6x8 bytes. Using RandomAccessFile, Will I use ReadInt, ReadFloat an ReadDouble and get same numbers? The question is related to bytes arrangement for numbers. If I remember well, Microsoft treats Low and High byte in a determinate way, to represent integers for example. Is it the same with Android? This is my worry. Of course I will give it a try
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Think you are referring to Endian (little/big) lots of things let you specify the Endian when reading Integer etc.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Thanks to all. My question was bad written. My fault, sorry. I was meaning the following: let's suppose I write some data with Windows, for example an long integer, followed by two floats and six doubles. I will have a "block" made by 4+2x4+6x8 bytes. Using RandomAccessFile, Will I use ReadInt, ReadFloat an ReadDouble and get same numbers? The question is related to bytes arrangement for numbers. If I remember well, Microsoft treats Low and High byte in a determinate way, to represent integers for example. Is it the same with Android? This is my worry. Of course I will give it a try
Why not use B4XSerializator? it is a cross-platform for data exchange.

 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
I think that on Windows side I don't have any B4XSerializator. Keep in mind that on Windows side I will use Microsoft C++ or C#
There have been a lot of tips given, maybe a "bit stupid" of me but why don't you use a standard open exchange format? A standard exchange platform was invented in the past precisely to solve all kinds of challenges that have come up in previous posts. A long time ago we used XML for this, nowadays it can be JSON which has good B4X support.

Either you have:
  1. An existing program output or
  2. You have written a program yourself in C++ or C# in the Windows environment.
In the first case you can use a hex editor to see how the binary data is written and you could convert your "read binary files" to JSON format yourself in Windows C++ or C# before you exchange it to another OS platform or develop a B4J program for it.

In the second case you have the freedom to choose how you write the data. If you then also choose the JSON format you will avoid a lot of misery because they are solved by the JSON library.

Please note that an open standard like JSON can be read by anyone. Depending on how you transport the data, it may be necessary to implement additional protection measures such as encryption in order to comply with some privacy legislation.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
There have been a lot of tips given, maybe a "bit stupid" of me but why don't you use a standard open exchange format? A standard exchange platform was invented in the past precisely to solve all kinds of challenges that have come up in previous posts. A long time ago we used XML for this, nowadays it can be JSON which has good B4X support.

Either you have:
  1. An existing program output or
  2. You have written a program yourself in C++ or C# in the Windows environment.
In the first case you can use a hex editor to see how the binary data is written and you could convert your "read binary files" to JSON format yourself in Windows C++ or C# before you exchange it to another OS platform or develop a B4J program for it.

In the second case you have the freedom to choose how you write the data. If you then also choose the JSON format you will avoid a lot of misery because they are solved by the JSON library.

Please note that an open standard like JSON can be read by anyone. Depending on how you transport the data, it may be necessary to implement additional protection measures such as encryption in order to comply with some privacy legislation.
Thanks for your suggestion,but unluckily you are missing my goal, which is, simply, speed. If I wanted to use Ascii (Text i mean) files I had nearly zero problems, not involving any JSON, XML etc. It is not a problem of "compatibility". The initial issue was : produce a binary with Windows and use it with B4A. Nothing else. Thanks anyway.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
You should be able to read the bytes from a binary file into an array and then as long as you know the length, type and endianness of each value you could pick them out into individual arrays and use ByterConverter to convert the bytes to concrete types.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
If speed is really important and your binary files are very large you can run into memory issues. In that case you could read a byte for byte at a time with a random file. We did that in the 80s with a micro computer with little memory.
 
Upvote 0

emexes

Expert
Licensed User
read a byte for byte at a time with a random file

Examples here:

B4X:
Dim raf As RandomAccessFile
raf.Initialize(File.DirInternal, "Points1.bin", True)
 
Dim b(8) As Byte
raf.ReadBytes(b, 0, b.Length, raf.CurrentPosition)

Dim bc as ByteConverter 
Dim NumPoints As Int = bc.LongsFromBytes(b)(0)
Log(NumPoints)
 
Dim b(12) As Byte    'to read 3 floats x 4 bytes each = 12 bytes total
Dim NumBytesRead As Int = raf.ReadBytes(b, 0, b.Length, raf.CurrentPosition)
If NumBytesRead < 12 Then Exit    'nice try, no cigar
Dim XYZ() As Float = bc.FloatsFromInts(b)    'array of three coordinate values
 
Last edited:
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
You should be able to read the bytes from a binary file into an array and then as long as you know the length, type and endianness of each value you could pick them out into individual arrays and use ByterConverter to convert the bytes to concrete types.
Hi. Forgive me, but, being bytes, a way to convert exists, of course. My initial question was "before re-inventing the wheel...", with which I meant exactly not to be forced to deal with byte conversions etc. I am grateful to everyone who commented on my post. I just want to point out that if the main objective is speed, reading data and making conversions, they may not be very effective, either in time sense or memory. Nevertheless the comments will be useful in other cases. Thanks again.
 
Upvote 0
Top