Android Question String in to binary file

Claudio57

Member
Licensed User
Longtime User
I'm new in B4A. I was pretty good at VB6.
I have a problem with binary files.
I can't write a string to a binary file.
I can't find a method to write a string.
How to?

Martina
 

Beja

Expert
Licensed User
Longtime User
can you post a simple example in vb6 that you want to port to b4a?
 
Upvote 0

Claudio57

Member
Licensed User
Longtime User
Hi Erel,
I do not know English and use the Google translator . I have a big problem. I have to make an application that works with binary files. Must work with binary files because the files are also managed by Windows applications written in VB6 .
In VB6 type is declared like this:

Type Classification
Name As String * 15
Points As Integer
Goal As Integer
end Type

and then there is an array like this:

Public Cla ( 1 To 20 ) As Classification

This array is saved in a binary file with a for next:

For i = 1 To 20
Put # 1 , Cla ( i)
Next i

and when it should be read:

For i = 1 To 20
Get # 5 , Cla ( i)
Next i

I tried to do it in B4A but I can not make it work:

Type Classification ( Name As String , Value As Short , goal As Short )

but when I write I do not know what to do. I tried with:

Dim a As Classification
a.initialize
a.name ="Juventus"
a.points=22
a.goal=19
raf.Initialize ( File.DirRootExternal , " Class.gsc " , False)
raf.WriteObject (a, False , raf.CurrentPosition )

Please if you can help me. Maybe show me a tutorial on the RAF.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I understand it. You should however know which encoding is used.

I assume that it is ASCII. What happens if the string is shorter than 15 characters?

You can do something like:
B4X:
Sub WriteClarification(raf As RandomAccessFile, c As ClarificationType)
 Dim b() As Byte = c.Name.GetBytes("ASCII")
 raf.WriteBytes(b, 0, b.Length, raf.CurrentPosition)
 for i = b.Length To 14
  raf.WriteByte(0, raf.CurrentPosition)
 Next
 raf.WriteInt(c.Point, raf.CurrentPosition)
 raf.WriteInt(c.Goal, raf.CurrentPosition)
End Sub
 
Upvote 0

Claudio57

Member
Licensed User
Longtime User
Erel
I understand it. You should however know which encoding is used.

I assume that it is ASCII. What happens if the string is shorter than 15 characters?

You can do something like:
B4X:
Sub WriteClarification(raf As RandomAccessFile, c As ClarificationType)
 Dim b() As Byte = c.Name.GetBytes("ASCII")
 raf.WriteBytes(b, 0, b.Length, raf.CurrentPosition)
 for i = b.Length To 14
  raf.WriteByte(0, raf.CurrentPosition)
 Next
 raf.WriteInt(c.Point, raf.CurrentPosition)
 raf.WriteInt(c.Goal, raf.CurrentPosition)
End Sub
Hi Erel,
the length of the file with only one record is 34. It should be 19 (15 Name, 2 Point and 2 Goal). Why?
 
Upvote 0

Claudio57

Member
Licensed User
Longtime User
Thanks Erel. There are successful. Now it works. I write and read. :)
Without your help I would have never succeeded.
 
Upvote 0

Claudio57

Member
Licensed User
Longtime User
I have a binary file written in VB6. There are some numbers that I wrote as an integer.
Now when I read them in B4A call them as Short. These values do not match. Instead of 1 reads 256 instead of 2 512 etc. How do I fix?
 
Upvote 0

Claudio57

Member
Licensed User
Longtime User
Il tuo esempio è molto bello e mi da anche degli spunti utili però il mio problema è che i file che ho scritti in VB6 devono essere gestiti sia da PC (Windows) che da Smartphone. I dati integer scritti nel file binario occupano 2 Bytes, gli Int di B4A 4, allora in B4A ho definito Short i tipi relativi ai punti etc. Quando vado a leggerli però mi ritorna dei valori diversi da quelli scritti. Non ho capito perchè e che cosa devo fare.
 
Upvote 0

Claudio57

Member
Licensed User
Longtime User
I still have this big problem: I want to read integers written in binary file from VB6 and I want to write them B4A to read it in VB6.
How to?
 
Upvote 0
Top